Skip to content

Commit 5f15255

Browse files
marbremgehre-amd
authored andcommitted
[mlir][EmitC] Introduce a CExpression trait (llvm#84177)
This adds a `CExpression` trait and replaces the `isCExpression()` function.
1 parent 138bf9c commit 5f15255

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MLIR_DIALECT_EMITC_IR_EMITC_H
1515

1616
#include "mlir/Bytecode/BytecodeOpInterface.h"
17+
#include "mlir/Dialect/EmitC/IR/EmitCTraits.h"
1718
#include "mlir/IR/Builders.h"
1819
#include "mlir/IR/BuiltinOps.h"
1920
#include "mlir/IR/BuiltinTypes.h"

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
4747
let assemblyFormat = "operands attr-dict `:` functional-type(operands, results)";
4848
}
4949

50+
// EmitC OpTrait
51+
def CExpression : NativeOpTrait<"emitc::CExpression">;
52+
5053
// Types only used in binary arithmetic operations.
5154
def IntegerIndexOrOpaqueType : AnyTypeOf<[AnyInteger, Index, EmitC_OpaqueType]>;
5255
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[AnyFloat, IntegerIndexOrOpaqueType]>;
5356

54-
def EmitC_AddOp : EmitC_BinaryOp<"add", []> {
57+
def EmitC_AddOp : EmitC_BinaryOp<"add", [CExpression]> {
5558
let summary = "Addition operation";
5659
let description = [{
5760
With the `add` operation the arithmetic operator + (addition) can
@@ -74,7 +77,7 @@ def EmitC_AddOp : EmitC_BinaryOp<"add", []> {
7477
let hasVerifier = 1;
7578
}
7679

77-
def EmitC_ApplyOp : EmitC_Op<"apply", []> {
80+
def EmitC_ApplyOp : EmitC_Op<"apply", [CExpression]> {
7881
let summary = "Apply operation";
7982
let description = [{
8083
With the `apply` operation the operators & (address of) and * (contents of)
@@ -211,7 +214,7 @@ def EmitC_BitwiseXorOp : EmitC_BinaryOp<"bitwise_xor", []> {
211214
}];
212215
}
213216

214-
def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", []> {
217+
def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
215218
let summary = "Opaque call operation";
216219
let description = [{
217220
The `call_opaque` operation represents a C++ function call. The callee
@@ -257,10 +260,10 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", []> {
257260
let hasVerifier = 1;
258261
}
259262

260-
def EmitC_CastOp : EmitC_Op<"cast", [
261-
DeclareOpInterfaceMethods<CastOpInterface>,
262-
SameOperandsAndResultShape
263-
]> {
263+
def EmitC_CastOp : EmitC_Op<"cast",
264+
[CExpression,
265+
DeclareOpInterfaceMethods<CastOpInterface>,
266+
SameOperandsAndResultShape]> {
264267
let summary = "Cast operation";
265268
let description = [{
266269
The `cast` operation performs an explicit type conversion and is emitted
@@ -284,7 +287,7 @@ def EmitC_CastOp : EmitC_Op<"cast", [
284287
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest)";
285288
}
286289

287-
def EmitC_CmpOp : EmitC_BinaryOp<"cmp", []> {
290+
def EmitC_CmpOp : EmitC_BinaryOp<"cmp", [CExpression]> {
288291
let summary = "Comparison operation";
289292
let description = [{
290293
With the `cmp` operation the comparison operators ==, !=, <, <=, >, >=, <=>
@@ -355,7 +358,7 @@ def EmitC_ConstantOp : EmitC_Op<"constant", [ConstantLike]> {
355358
let hasVerifier = 1;
356359
}
357360

358-
def EmitC_DivOp : EmitC_BinaryOp<"div", []> {
361+
def EmitC_DivOp : EmitC_BinaryOp<"div", [CExpression]> {
359362
let summary = "Division operation";
360363
let description = [{
361364
With the `div` operation the arithmetic operator / (division) can
@@ -409,9 +412,8 @@ def EmitC_ExpressionOp : EmitC_Op<"expression",
409412
int32_t v7 = foo(v1 + v2) * (v3 + v4);
410413
```
411414

412-
The operations allowed within expression body are `emitc.add`,
413-
`emitc.apply`, `emitc.call_opaque`, `emitc.cast`, `emitc.cmp`, `emitc.div`,
414-
`emitc.mul`, `emitc.rem`, and `emitc.sub`.
415+
The operations allowed within expression body are EmitC operations with the
416+
CExpression trait.
415417

416418
When specified, the optional `do_not_inline` indicates that the expression is
417419
to be emitted as seen above, i.e. as the rhs of an EmitC SSA value
@@ -427,14 +429,9 @@ def EmitC_ExpressionOp : EmitC_Op<"expression",
427429
let assemblyFormat = "attr-dict (`noinline` $do_not_inline^)? `:` type($result) $region";
428430

429431
let extraClassDeclaration = [{
430-
static bool isCExpression(Operation &op) {
431-
return isa<emitc::AddOp, emitc::ApplyOp, emitc::CallOpaqueOp,
432-
emitc::CastOp, emitc::CmpOp, emitc::DivOp, emitc::MulOp,
433-
emitc::RemOp, emitc::SubOp>(op);
434-
}
435432
bool hasSideEffects() {
436433
auto predicate = [](Operation &op) {
437-
assert(isCExpression(op) && "Expected a C expression");
434+
assert(op.hasTrait<OpTrait::emitc::CExpression>() && "Expected a C expression");
438435
// Conservatively assume calls to read and write memory.
439436
if (isa<emitc::CallOpaqueOp>(op))
440437
return true;
@@ -837,7 +834,7 @@ def EmitC_LogicalOrOp : EmitC_BinaryOp<"logical_or", []> {
837834
let assemblyFormat = "operands attr-dict `:` type(operands)";
838835
}
839836

840-
def EmitC_MulOp : EmitC_BinaryOp<"mul", []> {
837+
def EmitC_MulOp : EmitC_BinaryOp<"mul", [CExpression]> {
841838
let summary = "Multiplication operation";
842839
let description = [{
843840
With the `mul` operation the arithmetic operator * (multiplication) can
@@ -861,7 +858,7 @@ def EmitC_MulOp : EmitC_BinaryOp<"mul", []> {
861858
let results = (outs FloatIntegerIndexOrOpaqueType);
862859
}
863860

864-
def EmitC_RemOp : EmitC_BinaryOp<"rem", []> {
861+
def EmitC_RemOp : EmitC_BinaryOp<"rem", [CExpression]> {
865862
let summary = "Remainder operation";
866863
let description = [{
867864
With the `rem` operation the arithmetic operator % (remainder) can
@@ -883,7 +880,7 @@ def EmitC_RemOp : EmitC_BinaryOp<"rem", []> {
883880
let results = (outs IntegerIndexOrOpaqueType);
884881
}
885882

886-
def EmitC_SubOp : EmitC_BinaryOp<"sub", []> {
883+
def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
887884
let summary = "Subtraction operation";
888885
let description = [{
889886
With the `sub` operation the arithmetic operator - (subtraction) can
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- EmitCTraits.h - EmitC trait definitions ------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares C++ classes for some of the traits used in the EmitC
10+
// dialect.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_DIALECT_EMITC_IR_EMITCTRAITS_H
15+
#define MLIR_DIALECT_EMITC_IR_EMITCTRAITS_H
16+
17+
#include "mlir/IR/OpDefinition.h"
18+
19+
namespace mlir {
20+
namespace OpTrait {
21+
namespace emitc {
22+
23+
template <typename ConcreteType>
24+
class CExpression : public TraitBase<ConcreteType, CExpression> {};
25+
26+
} // namespace emitc
27+
} // namespace OpTrait
28+
} // namespace mlir
29+
30+
#endif // MLIR_DIALECT_EMITC_IR_EMITCTRAITS_H

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "mlir/Dialect/EmitC/IR/EmitC.h"
10+
#include "mlir/Dialect/EmitC/IR/EmitCTraits.h"
1011
#include "mlir/IR/Builders.h"
1112
#include "mlir/IR/BuiltinAttributes.h"
1213
#include "mlir/IR/DialectImplementation.h"
@@ -245,7 +246,7 @@ LogicalResult ExpressionOp::verify() {
245246
return emitOpError("requires yielded type to match return type");
246247

247248
for (Operation &op : region.front().without_terminator()) {
248-
if (!isCExpression(op))
249+
if (!op.hasTrait<OpTrait::emitc::CExpression>())
249250
return emitOpError("contains an unsupported operation");
250251
if (op.getNumResults() != 1)
251252
return emitOpError("requires exactly one result for each operation");

mlir/lib/Dialect/EmitC/Transforms/FormExpressions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct FormExpressionsPass
3636
// Wrap each C operator op with an expression op.
3737
OpBuilder builder(context);
3838
auto matchFun = [&](Operation *op) {
39-
if (emitc::ExpressionOp::isCExpression(*op))
39+
if (op->hasTrait<OpTrait::emitc::CExpression>())
4040
createExpression(op, builder);
4141
};
4242
rootOp->walk(matchFun);

mlir/lib/Dialect/EmitC/Transforms/Transforms.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace mlir {
1616
namespace emitc {
1717

1818
ExpressionOp createExpression(Operation *op, OpBuilder &builder) {
19-
assert(ExpressionOp::isCExpression(*op) && "Expected a C expression");
19+
assert(op->hasTrait<OpTrait::emitc::CExpression>() &&
20+
"Expected a C expression");
2021

2122
// Create an expression yielding the value returned by op.
2223
assert(op->getNumResults() == 1 && "Expected exactly one result");

0 commit comments

Comments
 (0)