diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h --- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h +++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h @@ -16,6 +16,7 @@ #include "mlir/Dialect/SPIRV/IR/SPIRVOpTraits.h" #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h" #include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/OpImplementation.h" #include "mlir/Interfaces/ControlFlowInterfaces.h" #include "mlir/Interfaces/SideEffectInterfaces.h" #include "llvm/Support/PointerLikeTypeTraits.h" diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVStructureOps.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVStructureOps.td --- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVStructureOps.td +++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVStructureOps.td @@ -16,6 +16,7 @@ #define MLIR_DIALECT_SPIRV_IR_STRUCTURE_OPS include "mlir/Dialect/SPIRV/IR/SPIRVBase.td" +include "mlir/IR/OpAsmInterface.td" include "mlir/IR/SymbolInterfaces.td" include "mlir/Interfaces/CallInterfaces.td" include "mlir/Interfaces/SideEffectInterfaces.td" @@ -67,7 +68,8 @@ // ----- -def SPV_ConstantOp : SPV_Op<"Constant", [ConstantLike, NoSideEffect]> { +def SPV_ConstantOp : SPV_Op<"Constant", + [ConstantLike, DeclareOpInterfaceMethods, NoSideEffect]> { let summary = "The op that declares a SPIR-V normal constant"; let description = [{ diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp --- a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp +++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp @@ -1650,6 +1650,46 @@ llvm_unreachable("unimplemented types for ConstantOp::getOne()"); } +void mlir::spirv::ConstantOp::getAsmResultNames( + llvm::function_ref setNameFn) { + Type type = getType(); + + SmallString<32> specialNameBuffer; + llvm::raw_svector_ostream specialName(specialNameBuffer); + specialName << "cst"; + + IntegerType intTy = type.dyn_cast(); + + if (IntegerAttr intCst = value().dyn_cast()) { + if (intTy && intTy.getWidth() == 1) { + return setNameFn(getResult(), (intCst.getInt() ? "true" : "false")); + } + + if (intTy.isSignless()) { + specialName << intCst.getInt(); + } else { + specialName << intCst.getSInt(); + } + } + + if (intTy || type.isa()) { + specialName << '_' << type; + } + + if (auto vecType = type.dyn_cast()) { + specialName << "_vec_"; + specialName << vecType.getDimSize(0); + + Type elementType = vecType.getElementType(); + + if (elementType.isa() || elementType.isa()) { + specialName << "x" << elementType; + } + } + + setNameFn(getResult(), specialName.str()); +} + //===----------------------------------------------------------------------===// // spv.EntryPoint //===----------------------------------------------------------------------===// diff --git a/mlir/test/Dialect/SPIRV/IR/asm-op-interface.mlir b/mlir/test/Dialect/SPIRV/IR/asm-op-interface.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/SPIRV/IR/asm-op-interface.mlir @@ -0,0 +1,28 @@ +// RUN: mlir-opt %s | FileCheck %s + +func @const() -> () { + // CHECK: %true + %0 = spv.Constant true + // CHECK: %false + %1 = spv.Constant false + + // CHECK: %cst42_i32 + %2 = spv.Constant 42 : i32 + // CHECK: %cst-42_i32 + %-2 = spv.Constant -42 : i32 + // CHECK: %cst43_i64 + %3 = spv.Constant 43 : i64 + + // CHECK: %cst_f32 + %4 = spv.Constant 0.5 : f32 + // CHECK: %cst_f64 + %5 = spv.Constant 0.5 : f64 + + // CHECK: %cst_vec_3xi32 + %6 = spv.Constant dense<[1, 2, 3]> : vector<3xi32> + + // CHECK: %cst + %8 = spv.Constant [dense<3.0> : vector<2xf32>] : !spv.array<1xvector<2xf32>> + + return +} diff --git a/mlir/test/Dialect/SPIRV/IR/memory-ops.mlir b/mlir/test/Dialect/SPIRV/IR/memory-ops.mlir --- a/mlir/test/Dialect/SPIRV/IR/memory-ops.mlir +++ b/mlir/test/Dialect/SPIRV/IR/memory-ops.mlir @@ -487,8 +487,9 @@ // ----- func @variable_init_normal_constant() -> () { + // CHECK: %[[cst:.*]] = spv.Constant %0 = spv.Constant 4.0 : f32 - // CHECK: spv.Variable init(%0) : !spv.ptr + // CHECK: spv.Variable init(%[[cst]]) : !spv.ptr %1 = spv.Variable init(%0) : !spv.ptr return }