Skip to content

Commit 4ff440b

Browse files
Simon Camphausenmarbre
authored andcommitted
[mlir] Change custom syntax of emitc.include op to resemble C
This changes the custom syntax of the emitc.include operation for standard includes. Reviewed By: marbre Differential Revision: https://reviews.llvm.org/D105281
1 parent 0724c0e commit 4ff440b

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,24 @@ def EmitC_IncludeOp
126126

127127
```mlir
128128
// Custom form defining the inclusion of `<myheader>`.
129-
emitc.include "myheader.h" is_standard_include
129+
emitc.include <"myheader.h">
130130

131131
// Generic form of the same operation.
132132
"emitc.include" (){include = "myheader.h", is_standard_include} : () -> ()
133133

134-
// Generic form defining the inclusion of `"myheader"`.
134+
// Custom form defining the inclusion of `"myheader"`.
135+
emitc.include "myheader.h"
136+
137+
// Generic form of the same operation.
135138
"emitc.include" (){include = "myheader.h"} : () -> ()
136139
```
137140
}];
138141
let arguments = (ins
139142
Arg<StrAttr, "source file to include">:$include,
140143
UnitAttr:$is_standard_include
141144
);
142-
let assemblyFormat = [{
143-
$include attr-dict (`is_standard_include` $is_standard_include^)?
144-
}];
145+
let printer = [{ return ::print(p, *this); }];
146+
let parser = [{ return ::parse$cppClass(parser, result); }];
145147
let verifier = ?;
146148
}
147149

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,41 @@ OpFoldResult emitc::ConstantOp::fold(ArrayRef<Attribute> operands) {
116116
return value();
117117
}
118118

119+
//===----------------------------------------------------------------------===//
120+
// IncludeOp
121+
//===----------------------------------------------------------------------===//
122+
123+
static void print(OpAsmPrinter &p, IncludeOp &op) {
124+
bool standardInclude = op.is_standard_include();
125+
126+
p << IncludeOp::getOperationName() << " ";
127+
if (standardInclude)
128+
p << "<";
129+
p << "\"" << op.include() << "\"";
130+
if (standardInclude)
131+
p << ">";
132+
}
133+
134+
static ParseResult parseIncludeOp(OpAsmParser &parser, OperationState &result) {
135+
bool standardInclude = !parser.parseOptionalLess();
136+
137+
StringAttr include;
138+
OptionalParseResult includeParseResult =
139+
parser.parseOptionalAttribute(include, "include", result.attributes);
140+
if (!includeParseResult.hasValue())
141+
return parser.emitError(parser.getNameLoc()) << "expected string attribute";
142+
143+
if (standardInclude && parser.parseOptionalGreater())
144+
return parser.emitError(parser.getNameLoc())
145+
<< "expected trailing '>' for standard include";
146+
147+
if (standardInclude)
148+
result.addAttribute("is_standard_include",
149+
UnitAttr::get(parser.getBuilder().getContext()));
150+
151+
return success();
152+
}
153+
119154
//===----------------------------------------------------------------------===//
120155
// TableGen'd op method definitions
121156
//===----------------------------------------------------------------------===//

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: mlir-opt -verify-diagnostics %s | FileCheck %s
1+
// RUN: mlir-opt %s | mlir-opt | FileCheck %s
22

3-
"emitc.include" (){include = "test.h", is_standard_include} : () -> ()
4-
emitc.include "test.h" is_standard_include
3+
emitc.include <"test.h">
4+
emitc.include "test.h"
55

66
// CHECK-LABEL: func @f(%{{.*}}: i32, %{{.*}}: !emitc.opaque<"int32_t">) {
77
func @f(%arg0: i32, %f: !emitc.opaque<"int32_t">) {

0 commit comments

Comments
 (0)