diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td --- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td +++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td @@ -45,7 +45,6 @@ ``` }]; - // TODO: Disallow to apply the operator & (address of) to emitc.constant operations. let arguments = (ins Arg:$applicableOperator, AnyType:$operand diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp --- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp +++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp @@ -59,6 +59,10 @@ if (applicableOperatorStr != "&" && applicableOperatorStr != "*") return emitOpError("applicable operator is illegal"); + Operation *op = getOperand().getDefiningOp(); + if (op && dyn_cast(op)) + return emitOpError("cannot apply to constant"); + return success(); } diff --git a/mlir/test/Dialect/EmitC/invalid_ops.mlir b/mlir/test/Dialect/EmitC/invalid_ops.mlir --- a/mlir/test/Dialect/EmitC/invalid_ops.mlir +++ b/mlir/test/Dialect/EmitC/invalid_ops.mlir @@ -80,6 +80,15 @@ // ----- +func.func @illegal_operand() { + %1 = "emitc.constant"(){value = 42: i32} : () -> i32 + // expected-error @+1 {{'emitc.apply' op cannot apply to constant}} + %2 = emitc.apply "&"(%1) : (i32) -> !emitc.ptr + return +} + +// ----- + func.func @var_attribute_return_type_1() { // expected-error @+1 {{'emitc.variable' op requires attribute's type ('i64') to match op's return type ('i32')}} %c0 = "emitc.variable"(){value = 42: i64} : () -> i32