Skip to content

[mlir][spirv] Enable (de)serialization of TensorARM to/from OpConstan… #151485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ LogicalResult
spirv::Deserializer::processConstantNull(ArrayRef<uint32_t> operands) {
if (operands.size() != 2) {
return emitError(unknownLoc,
"OpConstantNull must have type <id> and result <id>");
"OpConstantNull must only have type <id> and result <id>");
}

Type resultType = getType(operands[0]);
Expand All @@ -1789,8 +1789,15 @@ spirv::Deserializer::processConstantNull(ArrayRef<uint32_t> operands) {
}

auto resultID = operands[1];
Attribute attr;
if (resultType.isIntOrFloat() || isa<VectorType>(resultType)) {
auto attr = opBuilder.getZeroAttr(resultType);
attr = opBuilder.getZeroAttr(resultType);
} else if (auto tensorType = dyn_cast<TensorArmType>(resultType)) {
if (auto element = opBuilder.getZeroAttr(tensorType.getElementType()))
attr = DenseElementsAttr::get(tensorType, element);
}

if (attr) {
// For normal constants, we just record the attribute (and its type) for
// later materialization at use sites.
constantMap.try_emplace(resultID, attr, resultType);
Expand Down
37 changes: 32 additions & 5 deletions mlir/lib/Target/SPIRV/Serialization/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ static Block *getPhiIncomingBlock(Block *block) {
return block;
}

static bool isZeroValue(Attribute attr) {
if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
return floatAttr.getValue().isZero();
}
if (auto boolAttr = dyn_cast<BoolAttr>(attr)) {
return !boolAttr.getValue();
}
if (auto intAttr = dyn_cast<IntegerAttr>(attr)) {
return intAttr.getValue().isZero();
}
if (auto splatElemAttr = dyn_cast<SplatElementsAttr>(attr)) {
return isZeroValue(splatElemAttr.getSplatValue<Attribute>());
}
if (auto denseElemAttr = dyn_cast<DenseElementsAttr>(attr)) {
return all_of(denseElemAttr.getValues<Attribute>(), isZeroValue);
}
return false;
}

namespace mlir {
namespace spirv {

Expand Down Expand Up @@ -959,6 +978,11 @@ Serializer::prepareDenseElementsConstant(Location loc, Type constType,
return 0;
}
} else if (isa<spirv::TensorArmType>(constType)) {
if (isZeroValue(valueAttr)) {
encodeInstructionInto(typesGlobalValues, spirv::Opcode::OpConstantNull,
{typeID, resultID});
return resultID;
}
numberOfConstituents = shapedType.getNumElements();
operands.reserve(numberOfConstituents + 2);
for (int i = 0; i < numberOfConstituents; ++i) {
Expand Down Expand Up @@ -1202,11 +1226,14 @@ uint32_t Serializer::prepareConstantCompositeReplicate(Location loc,
}

uint32_t resultID = getNextID();
uint32_t operands[] = {typeID, resultID, constandID};

encodeInstructionInto(typesGlobalValues,
spirv::Opcode::OpConstantCompositeReplicateEXT,
operands);
if (dyn_cast<spirv::TensorArmType>(resultType) && isZeroValue(valueAttr)) {
encodeInstructionInto(typesGlobalValues, spirv::Opcode::OpConstantNull,
{typeID, resultID});
} else {
encodeInstructionInto(typesGlobalValues,
spirv::Opcode::OpConstantCompositeReplicateEXT,
{typeID, resultID, constandID});
}

constCompositeReplicateIDMap[valueTypePair] = resultID;
return resultID;
Expand Down
28 changes: 28 additions & 0 deletions mlir/test/Target/SPIRV/constant.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ spirv.module Logical Vulkan requires #spirv.vce<v1.3, [VulkanMemoryModel, Shader
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xf32>
}

// CHECK-LABEL: @null_arm_tensor_of_i32
spirv.func @null_arm_tensor_of_i32() -> (!spirv.arm.tensor<2x3xi32>) "None" {
// CHECK: spirv.Constant dense<0> : !spirv.arm.tensor<2x3xi32>
%0 = spirv.Constant dense<0> : !spirv.arm.tensor<2x3xi32>
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xi32>
}

// CHECK-LABEL: @null_arm_tensor_of_f32
spirv.func @null_arm_tensor_of_f32() -> (!spirv.arm.tensor<2x3xf32>) "None" {
// CHECK: spirv.Constant dense<0.000000e+00> : !spirv.arm.tensor<2x3xf32>
%0 = spirv.Constant dense<0.0> : !spirv.arm.tensor<2x3xf32>
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xf32>
}

spirv.EntryPoint "GLCompute" @bool_const
}

Expand Down Expand Up @@ -391,6 +405,13 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, ReplicatedCompos
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xi32>
}

// CHECK-LABEL: @null_cc_arm_tensor_of_i32
spirv.func @null_cc_arm_tensor_of_i32() -> (!spirv.arm.tensor<2x3xi32>) "None" {
// CHECK: spirv.Constant dense<0> : !spirv.arm.tensor<2x3xi32>
%0 = spirv.EXT.ConstantCompositeReplicate [0 : i32] : !spirv.arm.tensor<2x3xi32>
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xi32>
}

// CHECK-LABEL: @splat_vector_f32
spirv.func @splat_vector_f32() -> (vector<3xf32>) "None" {
// CHECK: spirv.EXT.ConstantCompositeReplicate [1.000000e+00 : f32] : vector<3xf32>
Expand Down Expand Up @@ -439,4 +460,11 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, ReplicatedCompos
%0 = spirv.EXT.ConstantCompositeReplicate [2.0 : f32] : !spirv.arm.tensor<2x3xf32>
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xf32>
}

// CHECK-LABEL: @null_cc_arm_tensor_of_f32
spirv.func @null_cc_arm_tensor_of_f32() -> (!spirv.arm.tensor<2x3xf32>) "None" {
// CHECK: spirv.Constant dense<0.000000e+00> : !spirv.arm.tensor<2x3xf32>
%0 = spirv.EXT.ConstantCompositeReplicate [0.0 : f32] : !spirv.arm.tensor<2x3xf32>
spirv.ReturnValue %0 : !spirv.arm.tensor<2x3xf32>
}
}