#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"
#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"
// -----
-def SPV_ConstantOp : SPV_Op<"Constant", [ConstantLike, NoSideEffect]> {
+def SPV_ConstantOp : SPV_Op<"Constant",
+ [ConstantLike, DeclareOpInterfaceMethods<OpAsmOpInterface>, NoSideEffect]> {
let summary = "The op that declares a SPIR-V normal constant";
let description = [{
llvm_unreachable("unimplemented types for ConstantOp::getOne()");
}
+void mlir::spirv::ConstantOp::getAsmResultNames(
+ llvm::function_ref<void(mlir::Value, llvm::StringRef)> setNameFn) {
+ Type type = getType();
+
+ SmallString<32> specialNameBuffer;
+ llvm::raw_svector_ostream specialName(specialNameBuffer);
+ specialName << "cst";
+
+ IntegerType intTy = type.dyn_cast<IntegerType>();
+
+ if (IntegerAttr intCst = value().dyn_cast<IntegerAttr>()) {
+ 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<FloatType>()) {
+ specialName << '_' << type;
+ }
+
+ if (auto vecType = type.dyn_cast<VectorType>()) {
+ specialName << "_vec_";
+ specialName << vecType.getDimSize(0);
+
+ Type elementType = vecType.getElementType();
+
+ if (elementType.isa<IntegerType>() || elementType.isa<FloatType>()) {
+ specialName << "x" << elementType;
+ }
+ }
+
+ setNameFn(getResult(), specialName.str());
+}
+
//===----------------------------------------------------------------------===//
// spv.EntryPoint
//===----------------------------------------------------------------------===//
--- /dev/null
+// 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
+}
// -----
func @variable_init_normal_constant() -> () {
+ // CHECK: %[[cst:.*]] = spv.Constant
%0 = spv.Constant 4.0 : f32
- // CHECK: spv.Variable init(%0) : !spv.ptr<f32, Function>
+ // CHECK: spv.Variable init(%[[cst]]) : !spv.ptr<f32, Function>
%1 = spv.Variable init(%0) : !spv.ptr<f32, Function>
return
}