Skip to content

Commit

Permalink
Merge pull request #385 from Xilinx/matthias.fix_verbatim_parsing
Browse files Browse the repository at this point in the history
Fix verbatim parsing to be unambiguous
  • Loading branch information
mgehre-amd authored Oct 10, 2024
2 parents 81b017a + 95521c7 commit b04eab8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
let builders = [OpBuilder<(ins "::mlir::StringAttr":$value), [{ build($_builder, $_state, value, {}); }] >];
let builders = [OpBuilder<(ins "::llvm::StringRef":$value), [{ build($_builder, $_state, value, {}); }] >];
let hasVerifier = 1;
let assemblyFormat = "$value ($fmtArgs^ `:` type($fmtArgs))? attr-dict";
let assemblyFormat = "$value (`args` $fmtArgs^ `:` type($fmtArgs))? attr-dict";
}

def EmitC_AssignOp : EmitC_Op<"assign", []> {
Expand Down
12 changes: 6 additions & 6 deletions mlir/test/Dialect/EmitC/invalid_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -481,46 +481,46 @@ emitc.global const @myref : !emitc.array<2xi16> ref

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op requires operands for each placeholder in the format string}}
emitc.verbatim "" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "" args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}

// -----

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op requires operands for each placeholder in the format string}}
emitc.verbatim "abc" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "abc" args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}

// -----

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op requires operands for each placeholder in the format string}}
emitc.verbatim "{}" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{}" args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}

// -----

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op requires operands for each placeholder in the format string}}
emitc.verbatim "{} {} {}" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{} {} {}" args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}

// -----

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op expected '}' after unescaped '{'}}
emitc.verbatim "{ " %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{ " args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}

// -----

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
// expected-error @+1 {{'emitc.verbatim' op expected '}' after unescaped '{'}}
emitc.verbatim "{a} " %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{a} " args %arg0, %arg1 : !emitc.ptr<i32>, i32
return
}
9 changes: 7 additions & 2 deletions mlir/test/Dialect/EmitC/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,15 @@ emitc.verbatim "typedef float f32;"
emitc.verbatim "{} { }"

func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
emitc.verbatim "{} + {};" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{} + {};" args %arg0, %arg1 : !emitc.ptr<i32>, i32

// Trailing '{' are ok and don't start a placeholder.
emitc.verbatim "{} + {} {" %arg0, %arg1 : !emitc.ptr<i32>, i32
emitc.verbatim "{} + {} {" args %arg0, %arg1 : !emitc.ptr<i32>, i32

// Check there is no ambiguity whether %a is the argument to the emitc.verbatim op.
emitc.verbatim "a"
%a = "emitc.constant"(){value = 42 : i32} : () -> i32

return
}

Expand Down
10 changes: 5 additions & 5 deletions mlir/test/Target/Cpp/verbatim.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ emitc.func @func(%arg: f32) {
%a = "emitc.variable"(){value = #emitc.opaque<"">} : () -> !emitc.array<3x7xi32>
// CHECK: int32_t [[A:[^ ]*]][3][7];

emitc.verbatim "{}" %arg : f32
emitc.verbatim "{}" args %arg : f32
// CHECK: [[V0]]

emitc.verbatim "{} {{a" %arg : f32
emitc.verbatim "{} {{a" args %arg : f32
// CHECK-NEXT: [[V0]] {a

emitc.verbatim "#pragma my var={} property" %arg : f32
emitc.verbatim "#pragma my var={} property" args %arg : f32
// CHECK-NEXT: #pragma my var=[[V0]] property

// Trailing '{' are printed as-is.
emitc.verbatim "#pragma my var={} {" %arg : f32
emitc.verbatim "#pragma my var={} {" args %arg : f32
// CHECK-NEXT: #pragma my var=[[V0]] {

emitc.verbatim "#pragma my2 var={} property" %a : !emitc.array<3x7xi32>
emitc.verbatim "#pragma my2 var={} property" args %a : !emitc.array<3x7xi32>
// CHECK-NEXT: #pragma my2 var=[[A]] property
emitc.return
}
Expand Down

0 comments on commit b04eab8

Please sign in to comment.