Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 679d718

Browse files
authored
Drop GetAddressOfOp in favor of ApplyOp (#133)
This replaces the `emitc.getaddressof` op in favor of the newly added `emitc.apply`. The `emitc.apply` op allows to specify the operator to apply (the applicable operator). This not only allows a "get address" using the & operator, as it also enables a "get content" via the * operator.
1 parent c00504c commit 679d718

7 files changed

Lines changed: 96 additions & 38 deletions

File tree

‎docs/EmitC.md‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,38 @@ Examples:
1818

1919
## Operation definition
2020

21+
### `emitc.apply` (::mlir::emitc::ApplyOp)
22+
23+
apply operation
24+
25+
26+
Syntax:
27+
28+
```
29+
operation ::= `emitc.apply` $applicableOperator `(` $operand `)` attr-dict `:` functional-type($operand, results)
30+
```
31+
32+
With the "apply" operation the operators & (address of) and * (contents of)
33+
can be applied to a single operand.
34+
35+
#### Attributes:
36+
37+
| Attribute | MLIR Type | Description |
38+
| :-------: | :-------: | ----------- |
39+
`applicableOperator` | ::mlir::StringAttr | string attribute
40+
41+
#### Operands:
42+
43+
| Operand | Description |
44+
| :-----: | ----------- |
45+
`operand` | any type
46+
47+
#### Results:
48+
49+
| Result | Description |
50+
| :----: | ----------- |
51+
`result` | any type
52+
2153
### `emitc.call` (::mlir::emitc::CallOp)
2254

2355
call operation
@@ -178,24 +210,6 @@ func @conditional_reduce(%buffer: memref<1024xf32>, %lb: index,
178210
| :----: | ----------- |
179211
`results` | any type
180212

181-
### `emitc.getaddressof` (::mlir::emitc::GetAddressOfOp)
182-
183-
get address of operation
184-
185-
The "getaddressof" operation gets the address via the & operator in C++.
186-
187-
#### Operands:
188-
189-
| Operand | Description |
190-
| :-----: | ----------- |
191-
`operand` | any type
192-
193-
#### Results:
194-
195-
| Result | Description |
196-
| :----: | ----------- |
197-
`result` | An opaque type
198-
199213
### `emitc.if` (::mlir::emitc::IfOp)
200214

201215
if-then-else operation

‎include/emitc/Dialect/EmitC/EmitC.td‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def OpaqueType : EmitC_Type<"Opaque"> {
7979
class EmitC_Op<string mnemonic, list<OpTrait> traits = []> :
8080
Op<EmitC_Dialect, mnemonic, traits>;
8181

82+
def EmitC_ApplyOp : EmitC_Op<"apply", []> {
83+
let summary = "apply operation";
84+
let description = [{
85+
With the "apply" operation the operators & (address of) and * (contents of)
86+
can be applied to a single operand.
87+
}];
88+
let arguments = (ins
89+
Arg<StrAttr, "the operator to apply">:$applicableOperator,
90+
AnyType:$operand);
91+
let results = (outs AnyType:$result);
92+
let assemblyFormat = [{
93+
$applicableOperator `(` $operand `)` attr-dict `:` functional-type($operand, results)
94+
}];
95+
let verifier = [{ return ::verify(*this); }];
96+
}
97+
8298
def EmitC_CallOp : EmitC_Op<"call", []> {
8399
let summary = "call operation";
84100
let description = [{
@@ -117,15 +133,6 @@ def EmitC_ConstOp : EmitC_Op<"const", [ConstantLike, NoSideEffect]> {
117133
let hasFolder = 1;
118134
}
119135

120-
def EmitC_GetAddressOfOp : EmitC_Op<"getaddressof", []> {
121-
let summary = "get address of operation";
122-
let description = [{
123-
The "getaddressof" operation gets the address via the & operator in C++.
124-
}];
125-
let arguments = (ins AnyType:$operand);
126-
let results = (outs OpaqueType:$result);
127-
}
128-
129136
def EmitC_ForOp : EmitC_Op<"for",
130137
[DeclareOpInterfaceMethods<LoopLikeOpInterface>,
131138
DeclareOpInterfaceMethods<RegionBranchOpInterface>,

‎lib/Dialect/EmitC/IR/EmitCDialect.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ void emitc::buildTerminatedBody(OpBuilder &builder, Location loc) {
5050
builder.create<emitc::YieldOp>(loc);
5151
}
5252

53+
//===----------------------------------------------------------------------===//
54+
// ApplyOp
55+
//===----------------------------------------------------------------------===//
56+
57+
static LogicalResult verify(mlir::emitc::ApplyOp op) {
58+
StringRef applicableOperator = op.applicableOperator();
59+
60+
// Applicable operator must not be empty.
61+
if (applicableOperator.empty()) {
62+
return op.emitOpError("applicable operator must not be empty");
63+
}
64+
65+
// Only `*` and `&` are supported.
66+
if (!applicableOperator.equals("&") && !applicableOperator.equals("*"))
67+
return op.emitOpError("applicable operator is illegal");
68+
69+
return success();
70+
}
71+
5372
//===----------------------------------------------------------------------===//
5473
// CallOp
5574
//===----------------------------------------------------------------------===//

‎lib/Target/Cpp/TranslateToCpp.cpp‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,14 @@ static LogicalResult printCallOp(CppEmitter &emitter, emitc::CallOp callOp) {
220220
return success();
221221
}
222222

223-
static LogicalResult printGetAddressOfOp(CppEmitter &emitter,
224-
emitc::GetAddressOfOp getAddressOfOp) {
223+
static LogicalResult printApplyOp(CppEmitter &emitter, emitc::ApplyOp applyOp) {
225224
auto &os = emitter.ostream();
226-
auto &op = *getAddressOfOp.getOperation();
225+
auto &op = *applyOp.getOperation();
227226

228227
if (failed(emitter.emitAssignPrefix(op)))
229228
return failure();
230-
os << "&";
231-
os << emitter.getOrCreateName(getAddressOfOp.getOperand());
229+
os << applyOp.applicableOperator();
230+
os << emitter.getOrCreateName(applyOp.getOperand());
232231

233232
return success();
234233
}
@@ -773,14 +772,14 @@ LogicalResult CppEmitter::emitLabel(Block &block) {
773772

774773
static LogicalResult printOperation(CppEmitter &emitter, Operation &op) {
775774
// EmitC ops.
775+
if (auto applyOp = dyn_cast<emitc::ApplyOp>(op))
776+
return printApplyOp(emitter, applyOp);
776777
if (auto callOp = dyn_cast<emitc::CallOp>(op))
777778
return printCallOp(emitter, callOp);
778779
if (auto constOp = dyn_cast<emitc::ConstOp>(op))
779780
return printConstantOp(emitter, constOp);
780781
if (auto forOp = dyn_cast<emitc::ForOp>(op))
781782
return printForOp(emitter, forOp);
782-
if (auto getAdressOfOp = dyn_cast<emitc::GetAddressOfOp>(op))
783-
return printGetAddressOfOp(emitter, getAdressOfOp);
784783
if (auto ifOp = dyn_cast<emitc::IfOp>(op))
785784
return printIfOp(emitter, ifOp);
786785
if (auto yieldOp = dyn_cast<emitc::YieldOp>(op))

‎test/Dialect/EmitC/invalid_ops.mlir‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ func @dense_template_argument(%arg : i32) {
6969
emitc.call "dense_template_argument"(%arg) {template_args = [dense<[1.0, 1.0]> : tensor<2xf32>]} : (i32) -> i32
7070
return
7171
}
72+
73+
// -----
74+
75+
func @empty_operator(%arg : i32) {
76+
// expected-error @+1 {{'emitc.apply' op applicable operator must not be empty}}
77+
%2 = emitc.apply ""(%arg) : (i32) -> !emitc.opaque<"int32_t*">
78+
return
79+
}
80+
81+
// -----
82+
83+
func @illegal_operator(%arg : i32) {
84+
// expected-error @+1 {{'emitc.apply' op applicable operator is illegal}}
85+
%2 = emitc.apply "+"(%arg) : (i32) -> !emitc.opaque<"int32_t*">
86+
return
87+
}

‎test/Dialect/EmitC/ops.mlir‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ func @c(%arg0: i32) {
1515
return
1616
}
1717

18-
func @a(%arg0: i32) {
19-
%1 = "emitc.getaddressof"(%arg0) : (i32) -> !emitc.opaque<"int32_t*">
18+
func @a(%arg0: i32, %arg1: i32) {
19+
%1 = "emitc.apply"(%arg0) {applicableOperator = "&"} : (i32) -> !emitc.opaque<"int32_t*">
20+
%2 = emitc.apply "&"(%arg1) : (i32) -> !emitc.opaque<"int32_t*">
2021
return
2122
}

‎test/Target/common-cpp.mlir‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ func @opaque_types(%arg0: !emitc.opaque<"bool">, %arg1: !emitc.opaque<"char">) -
7777
return %2 : !emitc.opaque<"status_t">
7878
}
7979

80-
func @get_address_of(%arg0: i32) -> !emitc.opaque<"int32_t*"> {
80+
func @apply(%arg0: i32) -> !emitc.opaque<"int32_t*"> {
8181
// CHECK: int32_t* [[V2]] = &[[V1]];
82-
%0 = "emitc.getaddressof" (%arg0) : (i32) -> !emitc.opaque<"int32_t*">
82+
%0 = emitc.apply "&"(%arg0) : (i32) -> !emitc.opaque<"int32_t*">
83+
// CHECK: int32_t [[V3]] = *[[V2]];
84+
%1 = emitc.apply "*"(%0) : (!emitc.opaque<"int32_t*">) -> (i32)
8385
return %0 : !emitc.opaque<"int32_t*">
8486
}
8587

0 commit comments

Comments
 (0)