Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/MLIR/Semantics/AttributeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class AttributeValue
/// <summary>
/// Initializes a new instance of the <see cref="AttributeValue"/> class.
/// </summary>
protected AttributeValue(RawSyntaxText syntax, string? name, AttributeDefinition? definition, SourceLocation location)
protected AttributeValue(AttributeValueSyntax? syntax, string? name, AttributeDefinition? definition, SourceLocation location)
{
Syntax = syntax;
Name = name;
Expand All @@ -20,9 +20,9 @@ protected AttributeValue(RawSyntaxText syntax, string? name, AttributeDefinition
}

/// <summary>
/// Gets the raw syntax text for the attribute value.
/// Gets the syntax for the attribute value, or null if this is a synthetic attribute value with no corresponding source text.
/// </summary>
public RawSyntaxText Syntax { get; }
public AttributeValueSyntax? Syntax { get; }

/// <summary>
/// Gets the canonical attribute name, if one was recognized.
Expand Down
6 changes: 3 additions & 3 deletions src/MLIR/Semantics/AttributeValueConstructionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace MLIR.Semantics;
/// </summary>
public sealed class AttributeValueConstructionContext
{
internal AttributeValueConstructionContext(RawSyntaxText syntax, string? name, AttributeDefinition definition, SourceLocation location)
internal AttributeValueConstructionContext(AttributeValueSyntax syntax, string? name, AttributeDefinition definition, SourceLocation location)
{
Syntax = syntax;
Name = name;
Expand All @@ -17,9 +17,9 @@ internal AttributeValueConstructionContext(RawSyntaxText syntax, string? name, A
}

/// <summary>
/// Gets the raw syntax text for the attribute value.
/// Gets the syntax for the attribute value.
/// </summary>
public RawSyntaxText Syntax { get; }
public AttributeValueSyntax Syntax { get; }

/// <summary>
/// Gets the canonical attribute name, if one was recognized.
Expand Down
4 changes: 2 additions & 2 deletions src/MLIR/Semantics/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ public AttributeValue BindAttributeValue(RawSyntaxText syntax)
var location = syntax.Location;
if (definition != null)
{
attribute = definition.Factory(new AttributeValueConstructionContext(syntax, canonicalName, definition, location));
attribute = definition.Factory(new AttributeValueConstructionContext(new RawAttributeValueSyntax(syntax), canonicalName, definition, location));
definition.AssemblyFormat?.Bind(attribute, new AttributeAssemblyBindingContext(attribute, diagnostics));
}
else
{
attribute = new UnknownAttributeValue(syntax, canonicalName, definition, location);
attribute = new UnknownAttributeValue(new RawAttributeValueSyntax(syntax), canonicalName, definition, location);
}

return attribute;
Expand Down
57 changes: 40 additions & 17 deletions src/MLIR/Semantics/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,61 @@ namespace MLIR.Semantics;
/// <summary>
/// Represents a semantic block within a region.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="Block"/> class.
/// </remarks>
/// <param name="syntax">The concrete syntax node for the block.</param>
/// <param name="arguments">The semantic block arguments.</param>
/// <param name="operations">The operations contained in the block.</param>
public sealed class Block(BlockSyntax syntax, IReadOnlyList<BlockArgument> arguments, IReadOnlyList<Operation> operations)
public sealed class Block
{
/// <summary>
/// Gets the concrete syntax node for the block.
/// Initializes a new instance of the <see cref="Block"/> class from a concrete syntax node.
/// The block label reference is derived from the syntax node's label token.
/// </summary>
public BlockSyntax Syntax { get; } = syntax;
/// <param name="syntax">The concrete syntax node for the block.</param>
/// <param name="arguments">The semantic block arguments.</param>
/// <param name="operations">The operations contained in the block.</param>
public Block(BlockSyntax syntax, IReadOnlyList<BlockArgument> arguments, IReadOnlyList<Operation> operations)
{
Syntax = syntax;
LabelReference = new BlockReference(syntax.LabelToken);
Arguments = arguments;
Operations = operations;
}

/// <summary>
/// Gets the semantic block arguments.
/// Initializes a new instance of the <see cref="Block"/> class as a synthetic block with no corresponding source text.
/// </summary>
public IReadOnlyList<BlockArgument> Arguments { get; } = arguments;
/// <param name="labelReference">The semantic reference to the block label.</param>
/// <param name="arguments">The semantic block arguments.</param>
/// <param name="operations">The operations contained in the block.</param>
public Block(BlockReference labelReference, IReadOnlyList<BlockArgument> arguments, IReadOnlyList<Operation> operations)
{
Syntax = null;
LabelReference = labelReference;
Arguments = arguments;
Operations = operations;
}

/// <summary>
/// Gets the operations contained in the block.
/// Gets the concrete syntax node for the block, or null if this is a synthetic block with no corresponding source text.
/// </summary>
public IReadOnlyList<Operation> Operations { get; } = operations;
public BlockSyntax? Syntax { get; }

/// <summary>
/// Gets the block label, including the leading <c>^</c>.
/// Gets the semantic reference to the block label.
/// </summary>
public BlockReference LabelReference { get; }

/// <summary>
/// Gets the semantic block arguments.
/// </summary>
public IReadOnlyList<BlockArgument> Arguments { get; }

/// <summary>
/// Gets the operations contained in the block.
/// </summary>
public string Label => Syntax.Label;
public IReadOnlyList<Operation> Operations { get; }

/// <summary>
/// Gets the typed reference to the block label.
/// Gets the block label, including the leading <c>^</c>.
/// </summary>
public BlockReference LabelReference { get; } = new BlockReference(syntax.LabelToken);
public string Label => LabelReference.Label;

/// <summary>
/// Gets the source location of the block label, if known.
Expand Down
6 changes: 3 additions & 3 deletions src/MLIR/Semantics/BlockReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public BlockReference(SyntaxToken token)
}

/// <summary>
/// Initializes a new instance of the <see cref="BlockReference"/> struct from a label text.
/// Initializes a new instance of the <see cref="BlockReference"/> struct for a synthetic reference with no corresponding source token.
/// </summary>
/// <param name="label">The block label text.</param>
/// <param name="label">The block label text, including the leading <c>^</c>.</param>
public BlockReference(string label)
{
Token = null;
Label = label;
}

/// <summary>
/// Gets the syntax token for the block label.
/// Gets the syntax token for the block label, or null if this is a synthetic reference with no corresponding source token.
/// </summary>
public SyntaxToken? Token { get; }

Expand Down
13 changes: 6 additions & 7 deletions src/MLIR/Semantics/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace MLIR.Semantics;

using System;
using System.Collections.Generic;
using MLIR.Construction;
using MLIR.Dialects;
using MLIR.Syntax;

Expand All @@ -15,7 +14,7 @@ public abstract class Operation
/// Initializes a new instance of the <see cref="Operation"/> class.
/// </summary>
protected Operation(
OperationSyntax syntax,
OperationSyntax? syntax,
string name,
OperationDefinition? definition)
{
Expand All @@ -25,9 +24,9 @@ protected Operation(
}

/// <summary>
/// Gets the concrete syntax node for the operation.
/// Gets the concrete syntax node for the operation, or null if this is a synthetic operation with no corresponding source text.
/// </summary>
public OperationSyntax Syntax { get; }
public OperationSyntax? Syntax { get; }

/// <summary>
/// Gets the canonical operation name without MLIR string-literal quoting.
Expand Down Expand Up @@ -75,9 +74,9 @@ protected Operation(
public bool IsKnown => Definition != null;

/// <summary>
/// Gets the operation name exactly as written in the source.
/// Gets the operation name exactly as written in the source, or null if this is a synthetic operation with no corresponding source text.
/// </summary>
public string SyntaxName => Syntax.Name;
public string? SyntaxName => Syntax?.Name;

/// <summary>
/// Gets the dialect namespace portion of the operation name, if present.
Expand Down Expand Up @@ -109,7 +108,7 @@ public string DialectName
/// <summary>
/// Gets the source location of the operation name, if known.
/// </summary>
public SourceLocation Location => SourceLocation.FromToken(Syntax.NameToken);
public SourceLocation Location => Syntax != null ? SourceLocation.FromToken(Syntax.NameToken) : SourceLocation.Unknown;

/// <summary>
/// Determines whether the operation has an attribute with the supplied name.
Expand Down
8 changes: 4 additions & 4 deletions src/MLIR/Semantics/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace MLIR.Semantics;
/// <remarks>
/// Initializes a new instance of the <see cref="Region"/> class.
/// </remarks>
/// <param name="syntax">The concrete syntax node for the region.</param>
/// <param name="syntax">The concrete syntax node for the region, or null for a synthetic region with no corresponding source text.</param>
/// <param name="blocks">The semantic blocks contained in the region.</param>
public sealed class Region(RegionSyntax syntax, IReadOnlyList<Block> blocks)
public sealed class Region(RegionSyntax? syntax, IReadOnlyList<Block> blocks)
{
/// <summary>
/// Gets the concrete syntax node for the region.
/// Gets the concrete syntax node for the region, or null if this is a synthetic region with no corresponding source text.
/// </summary>
public RegionSyntax Syntax { get; } = syntax;
public RegionSyntax? Syntax { get; } = syntax;

/// <summary>
/// Gets the semantic blocks contained in the region.
Expand Down
2 changes: 1 addition & 1 deletion src/MLIR/Semantics/UnknownAttributeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class UnknownAttributeValue : AttributeValue
/// <summary>
/// Initializes a new instance of the <see cref="UnknownAttributeValue"/> class.
/// </summary>
public UnknownAttributeValue(RawSyntaxText syntax, string? name, AttributeDefinition? definition, SourceLocation location)
public UnknownAttributeValue(AttributeValueSyntax syntax, string? name, AttributeDefinition? definition, SourceLocation location)
: base(syntax, name, definition, location)
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/MLIR/Semantics/ValueReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ValueReference(SyntaxToken token)
}

/// <summary>
/// Initializes a new instance of the <see cref="ValueReference"/> struct from a value name.
/// Initializes a new instance of the <see cref="ValueReference"/> struct for a synthetic value with no corresponding source token.
/// </summary>
/// <param name="name">The SSA value name.</param>
public ValueReference(string name)
Expand All @@ -28,7 +28,7 @@ public ValueReference(string name)
}

/// <summary>
/// Gets the syntax token for the SSA value.
/// Gets the syntax token for the SSA value, or null if this is a synthetic reference with no corresponding source token.
/// </summary>
public SyntaxToken? Token { get; }

Expand Down
91 changes: 79 additions & 12 deletions src/MLIR/Transforms/AssemblySyntaxBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace MLIR.Transforms;

using System;
using System.Collections.Generic;
using MLIR.Construction;
using MLIR.Semantics;
Expand Down Expand Up @@ -45,11 +46,36 @@

public OperationSyntax RewriteOperation(Operation operation, OperationBodySyntax body, SyntaxToken? nameToken = null)
{
if (operation.Syntax != null)
{
return new OperationSyntax(
operation.Syntax.ResultTokens,
operation.Syntax.ResultCommaTokens,
operation.Syntax.EqualsToken,
nameToken ?? operation.Syntax.NameToken,
body);
}

// Synthesize tokens for a synthetic operation with no source syntax.
var results = operation.Results;
var resultTokens = new List<SyntaxToken>(results.Count);
foreach (var result in results)
{
resultTokens.Add(new SyntaxToken(result));
}

var resultCommaTokens = new List<SyntaxToken>(Math.Max(0, results.Count - 1));
for (var i = 1; i < results.Count; i++)
{
resultCommaTokens.Add(new SyntaxToken(","));
}

var equalsToken = results.Count > 0 ? (SyntaxToken?)new SyntaxToken("=") : null;
return new OperationSyntax(
operation.Syntax.ResultTokens,
operation.Syntax.ResultCommaTokens,
operation.Syntax.EqualsToken,
nameToken ?? operation.Syntax.NameToken,
resultTokens,
resultCommaTokens,
equalsToken,
nameToken ?? new SyntaxToken(operation.Name),
body);
}

Expand All @@ -73,7 +99,7 @@

private GenericOperationBodySyntax GetGenericBody(Operation operation)
{
if (operation.Syntax.Body is GenericOperationBodySyntax genericBody)

Check warning on line 102 in src/MLIR/Transforms/AssemblySyntaxBuilder.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 102 in src/MLIR/Transforms/AssemblySyntaxBuilder.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
{
return genericBody;
}
Expand All @@ -84,12 +110,42 @@
operation.Results,
operation.Operands,
operation.Successors,
operation.Regions.Select(r => r.Syntax).ToList(),
operation.Attributes.Select(a => Factory.Attr(a.Name, a.Value.Syntax.Text)).ToList(),
operation.Regions.Select(BuildRegion).ToList(),
operation.Attributes.Select(BuildNamedAttribute).ToList(),
operation.TypeSignatureReference != null ? operation.TypeSignatureReference.Syntax : null
).Body;
}

public NamedAttributeSyntax BuildNamedAttribute(NamedAttribute attribute)
{
if (attribute.Syntax != null)
{
return attribute.Syntax;
}

// Synthesize an attribute syntax for a synthetic attribute with no source syntax.
return new NamedAttributeSyntax(
new SyntaxToken(attribute.Name),
new SyntaxToken("="),
BuildAttributeValue(attribute.Value));
}

public AttributeValueSyntax BuildAttributeValue(AttributeValue attributeValue)
{
if (attributeValue.Syntax != null)
{
return attributeValue.Syntax;
}

if (attributeValue is UnknownAttributeValue unknownAttributeValue)
{
// For unknown attribute values, we want to preserve the original syntax if possible, even if it was not recognized as a valid attribute value.
return unknownAttributeValue.Syntax!;
}

throw new InvalidOperationException($"Cannot build syntax for unrecognized attribute value of type {attributeValue.GetType().FullName}.");
}

public RegionSyntax BuildRegion(Region region)
{
var blocks = new List<BlockSyntax>(region.Blocks.Count);
Expand All @@ -98,7 +154,10 @@
blocks.Add(BuildBlock(block));
}

return new RegionSyntax(region.Syntax.OpenBraceToken, blocks, region.Syntax.CloseBraceToken);
return new RegionSyntax(
region.Syntax?.OpenBraceToken ?? new SyntaxToken("{"),
blocks,
region.Syntax?.CloseBraceToken ?? new SyntaxToken("}"));
}

public BlockSyntax BuildBlock(Block block)
Expand All @@ -109,11 +168,19 @@
operations.Add(BuildOperation(operation));
}

return new BlockSyntax(
block.Syntax.LabelToken,
block.Syntax.Arguments,
block.Syntax.ColonToken,
operations);
if (block.Syntax != null)
{
return new BlockSyntax(
block.Syntax.LabelToken,
block.Syntax.Arguments,
block.Syntax.ColonToken,
operations);
}

// Synthesize a block syntax for a synthetic block with no source syntax.
// Use "^entry" as the fallback label: the parser uses this synthetic label for implicit
// entry blocks, and BlockSyntax.WriteTo omits it during printing when there are no arguments.
return new BlockSyntax(block.Label, [], operations);
}
}
}
Loading
Loading