Skip to content

Commit 6f69fa7

Browse files
committed
Fix on SIR Dialect and Lowering
1 parent 87bcda0 commit 6f69fa7

File tree

14 files changed

+2482
-860
lines changed

14 files changed

+2482
-860
lines changed

build.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ fn buildMlirLibrariesImpl(step: *std.Build.Step, options: std.Build.Step.MakeOpt
616616
"-DLLVM_ENABLE_TERMINFO=OFF",
617617
"-DLLVM_ENABLE_RTTI=ON",
618618
"-DLLVM_ENABLE_EH=ON",
619+
"-DLLVM_ENABLE_PIC=ON",
619620
"-DLLVM_BUILD_LLVM_DYLIB=OFF",
620621
"-DLLVM_LINK_LLVM_DYLIB=OFF",
621622
"-DLLVM_BUILD_TOOLS=ON", // needed for tblgen

src/main.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn main() !void {
9090
const emit_typed_ast: bool = parsed.emit_typed_ast;
9191
const emit_typed_ast_format: ?[]const u8 = parsed.emit_typed_ast_format;
9292
var emit_mlir: bool = parsed.emit_mlir;
93-
const emit_mlir_sir: bool = parsed.emit_mlir_sir;
93+
var emit_mlir_sir: bool = parsed.emit_mlir_sir;
9494
const emit_cfg: bool = parsed.emit_cfg;
9595
const emit_abi: bool = parsed.emit_abi;
9696
const emit_abi_solidity: bool = parsed.emit_abi_solidity;
@@ -146,7 +146,9 @@ pub fn main() !void {
146146
emit_mlir = true; // Default: emit MLIR
147147
}
148148
// By default, emit Ora MLIR plus SIR MLIR when --emit-mlir is requested.
149-
// emit-mlir should stop after Ora MLIR (and validation) unless explicitly asked for SIR
149+
if (emit_mlir and !emit_mlir_sir) {
150+
emit_mlir_sir = true;
151+
}
150152

151153
// create MLIR options structure
152154
const mlir_options = MlirOptions{

src/mlir/IR/include/SIRDialect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "mlir/IR/Dialect.h"
1111
#include "mlir/IR/OpDefinition.h"
12+
#include "mlir/IR/SymbolTable.h"
1213
#include "mlir/Interfaces/SideEffectInterfaces.h"
1314
#include "mlir/Bytecode/BytecodeOpInterface.h"
1415

src/mlir/IR/td/SIROps.td

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ def StringConstantOp : SIR_Op<"string.constant"> {
5050
let assemblyFormat = "$value attr-dict `:` type($res)";
5151
}
5252

53+
// ==========================================================================
54+
// Metadata
55+
// ==========================================================================
56+
57+
def ErrorDeclOp : SIR_Op<"error.decl", [NoMemoryEffect]> {
58+
let summary = "Error declaration metadata";
59+
let arguments = (ins StrAttr:$sym_name);
60+
let assemblyFormat = "attr-dict";
61+
}
62+
5363
// ==========================================================================
5464
// Arithmetic Operations (binary, word-wise)
5565
// ==========================================================================
@@ -791,33 +801,62 @@ def StaticCallOp : SIR_Op<"staticcall"> {
791801
// Return & termination ops.
792802
// You already have a ReturnOp, we keep it and add stop/revert/invalid/selfdestruct.
793803

804+
// CFG ops.
805+
806+
def BrOp : SIR_Op<"br", [Terminator]> {
807+
let summary = "Unconditional branch";
808+
let arguments = (ins Variadic<AnyType>:$destOperands);
809+
let successors = (successor AnySuccessor:$dest);
810+
let assemblyFormat =
811+
"$dest (`(` $destOperands^ `:` type($destOperands) `)`)? attr-dict";
812+
}
813+
814+
def CondBrOp : SIR_Op<"cond_br", [Terminator, AttrSizedOperandSegments]> {
815+
let summary = "Conditional branch";
816+
let arguments = (ins SIR_U256:$cond,
817+
Variadic<AnyType>:$trueOperands,
818+
Variadic<AnyType>:$falseOperands);
819+
let successors = (successor AnySuccessor:$trueDest, AnySuccessor:$falseDest);
820+
let assemblyFormat =
821+
"$cond `:` type($cond) `,` $trueDest (`(` $trueOperands^ `:` type($trueOperands) `)`)? `,` "
822+
"$falseDest (`(` $falseOperands^ `:` type($falseOperands) `)`)? attr-dict";
823+
}
824+
825+
def SwitchOp : SIR_Op<"switch", [Terminator]> {
826+
let summary = "Switch on selector";
827+
let arguments = (ins SIR_U256:$selector, I64ArrayAttr:$caseValues);
828+
let successors = (successor AnySuccessor:$defaultDest, VariadicSuccessor<AnySuccessor>:$caseDests);
829+
let assemblyFormat =
830+
"$selector `:` type($selector) `,` `[` $caseValues `]` `,` $caseDests `,` $defaultDest attr-dict";
831+
}
832+
794833
def ReturnOp : SIR_Op<"return", [Terminator]> {
795834
let summary = "Return a pointer + size";
796835
let arguments = (ins SIR_Ptr:$ptr, SIR_U256:$len);
797836
let assemblyFormat =
798837
"$ptr `:` type($ptr) `,` $len `:` type($len) attr-dict";
799838
}
800839

801-
def StopOp : SIR_Op<"stop"> {
840+
def StopOp : SIR_Op<"stop", [Terminator]> {
802841
let summary = "STOP";
803842
let arguments = (ins);
804843
let assemblyFormat = "attr-dict";
805844
}
806845

807-
def RevertOp : SIR_Op<"revert"> {
846+
def RevertOp : SIR_Op<"revert", [Terminator]> {
808847
let summary = "REVERT";
809848
let arguments = (ins SIR_Ptr:$ptr, SIR_U256:$len);
810849
let assemblyFormat =
811850
"$ptr `:` type($ptr) `,` $len `:` type($len) attr-dict";
812851
}
813852

814-
def InvalidOp : SIR_Op<"invalid"> {
853+
def InvalidOp : SIR_Op<"invalid", [Terminator]> {
815854
let summary = "INVALID";
816855
let arguments = (ins);
817856
let assemblyFormat = "attr-dict";
818857
}
819858

820-
def SelfDestructOp : SIR_Op<"selfdestruct"> {
859+
def SelfDestructOp : SIR_Op<"selfdestruct", [Terminator]> {
821860
let summary = "SELFDESTRUCT";
822861
let arguments = (ins SIR_U256:$beneficiary);
823862
let assemblyFormat =
@@ -870,7 +909,7 @@ def NoOpOp : SIR_Op<"noop"> {
870909
// Internal Return
871910
// ==========================================================================
872911

873-
def IRetOp : SIR_Op<"iret"> {
912+
def IRetOp : SIR_Op<"iret", [Terminator]> {
874913
let summary = "Internal function return";
875914
let arguments = (ins Variadic<SIR_U256Type>:$values);
876915
let assemblyFormat =

src/mlir/ora/lib/OraCAPI.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ void oraBlockAppendOwnedOperation(MlirBlock block, MlirOperation op)
200200
return;
201201
}
202202

203+
if (auto contractOp = llvm::dyn_cast<mlir::ora::ContractOp>(operation))
204+
{
205+
if (auto moduleOp = llvm::dyn_cast<mlir::ModuleOp>(blk->getParentOp()))
206+
{
207+
moduleOp->setLoc(contractOp.getLoc());
208+
}
209+
}
210+
203211
if (blk->empty())
204212
{
205213
blk->push_back(operation);
@@ -762,6 +770,7 @@ MlirModule oraLowerContractStub(MlirContext ctx, MlirLocation loc, MlirStringRef
762770
builder.setInsertionPointToStart(moduleOp.getBody());
763771

764772
auto contractOp = builder.create<ContractOp>(location, contractRef);
773+
moduleOp->setLoc(contractOp.getLoc());
765774
auto &bodyRegion = contractOp.getBody();
766775
if (bodyRegion.empty())
767776
{
@@ -858,6 +867,7 @@ MlirModule oraLowerContractStubWithSig(
858867
builder.setInsertionPointToStart(moduleOp.getBody());
859868

860869
auto contractOp = builder.create<ContractOp>(location, contractRef);
870+
moduleOp->setLoc(contractOp.getLoc());
861871
auto &bodyRegion = contractOp.getBody();
862872
if (bodyRegion.empty())
863873
{

0 commit comments

Comments
 (0)