Skip to content

Commit 595a273

Browse files
authored
[mlir][emitc] Support 'emitc::LValueType' in 'emitc::VerbatimOp' (#144151)
This PR introduces support for `emitc::LvalueType` in `emitc::VerbatimOp`, providing a mechanism to reduce the number of operations required when working with verbatim operations whose arguments are of type `emitc::LvalueType`. Before: ```mlir emitc.func @foo() { %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32> %loaded_a = load %a : !emitc.lvalue<i32> emitc.verbatim "{} + {};" args %loaded_a, %loaded_a : i32, i32 return } ``` After: ```mlir emitc.func @bar() { %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32> emitc.verbatim "{} + {};" args %a, %a : !emitc.lvalue<i32>, !emitc.lvalue<i32> return } ``` You can now write something like this: ```mlir emitc.func @baz() { %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32> emitc.verbatim "++{};" args %a : !emitc.lvalue<i32> return } ```
1 parent 39ad315 commit 595a273

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
13041304
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
13051305
}];
13061306

1307-
let arguments = (ins StrAttr:$value, Variadic<EmitCType>:$fmtArgs);
1307+
let arguments = (ins StrAttr:$value, Variadic<AnyTypeOf<[EmitCType, EmitC_LValueType]>>:$fmtArgs);
13081308

13091309
let builders = [OpBuilder<(ins "::mlir::StringAttr":$value),
13101310
[{ build($_builder, $_state, value, {}); }]>];

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,20 @@ emitc.verbatim "typedef float f32;"
246246
// The value is not interpreted as format string if there are no operands.
247247
emitc.verbatim "{} { }"
248248

249-
func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
249+
func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32, %arg2: !emitc.array<3x!emitc.ptr<i32>>) {
250+
%a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32>
251+
252+
// Check that the lvalue type can be used by verbatim.
253+
emitc.verbatim "++{};" args %a : !emitc.lvalue<i32>
254+
255+
// Check that the array type can be used by verbatim.
256+
emitc.verbatim "*{}[0] = 1;" args %arg2 : !emitc.array<3x!emitc.ptr<i32>>
257+
250258
emitc.verbatim "{} + {};" args %arg0, %arg1 : !emitc.ptr<i32>, i32
251259

252-
// Check there is no ambiguity whether %a is the argument to the emitc.verbatim op.
253-
emitc.verbatim "a"
254-
%a = "emitc.constant"(){value = 42 : i32} : () -> i32
260+
// Check there is no ambiguity whether %b is the argument to the emitc.verbatim op.
261+
emitc.verbatim "b"
262+
%b = "emitc.constant"(){value = 42 : i32} : () -> i32
255263

256264
return
257265
}

0 commit comments

Comments
 (0)