Skip to content

[mlir][spirv] Replace hardcoded attribute strings with op methods Resolve #77627 #81443

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 4 commits into from
Feb 18, 2024
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
3 changes: 2 additions & 1 deletion mlir/lib/Target/SPIRV/Deserialization/DeserializeOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ Deserializer::processOp<spirv::CopyMemoryOp>(ArrayRef<uint32_t> words) {
auto attrValue = words[wordIndex++];
auto attr = opBuilder.getAttr<spirv::MemoryAccessAttr>(
static_cast<spirv::MemoryAccess>(attrValue));
attributes.push_back(opBuilder.getNamedAttr("memory_access", attr));
attributes.push_back(
opBuilder.getNamedAttr(attributeName<MemoryAccess>(), attr));
isAlignedAttr = (attrValue == 2);
}

Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Target/SPIRV/Deserialization/Deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ spirv::Deserializer::processMemoryModel(ArrayRef<uint32_t> operands) {
return emitError(unknownLoc, "OpMemoryModel must have two operands");

(*module)->setAttr(
"addressing_model",
module->getAddressingModelAttrName(),
opBuilder.getAttr<spirv::AddressingModelAttr>(
static_cast<spirv::AddressingModel>(operands.front())));
(*module)->setAttr("memory_model",

(*module)->setAttr(module->getMemoryModelAttrName(),
opBuilder.getAttr<spirv::MemoryModelAttr>(
static_cast<spirv::MemoryModel>(operands.back())));

Expand Down
20 changes: 12 additions & 8 deletions mlir/lib/Target/SPIRV/Serialization/SerializeOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,33 +709,37 @@ Serializer::processOp<spirv::CopyMemoryOp>(spirv::CopyMemoryOp op) {
operands.push_back(id);
}

if (auto attr = op->getAttr("memory_access")) {
StringAttr memoryAccess = op.getMemoryAccessAttrName();
if (auto attr = op->getAttr(memoryAccess)) {
operands.push_back(
static_cast<uint32_t>(cast<spirv::MemoryAccessAttr>(attr).getValue()));
}

elidedAttrs.push_back("memory_access");
elidedAttrs.push_back(memoryAccess.strref());

if (auto attr = op->getAttr("alignment")) {
StringAttr alignment = op.getAlignmentAttrName();
if (auto attr = op->getAttr(alignment)) {
operands.push_back(static_cast<uint32_t>(
cast<IntegerAttr>(attr).getValue().getZExtValue()));
}

elidedAttrs.push_back("alignment");
elidedAttrs.push_back(alignment.strref());

if (auto attr = op->getAttr("source_memory_access")) {
StringAttr sourceMemoryAccess = op.getSourceMemoryAccessAttrName();
if (auto attr = op->getAttr(sourceMemoryAccess)) {
operands.push_back(
static_cast<uint32_t>(cast<spirv::MemoryAccessAttr>(attr).getValue()));
}

elidedAttrs.push_back("source_memory_access");
elidedAttrs.push_back(sourceMemoryAccess.strref());

if (auto attr = op->getAttr("source_alignment")) {
StringAttr sourceAlignment = op.getSourceAlignmentAttrName();
if (auto attr = op->getAttr(sourceAlignment)) {
operands.push_back(static_cast<uint32_t>(
cast<IntegerAttr>(attr).getValue().getZExtValue()));
}

elidedAttrs.push_back("source_alignment");
elidedAttrs.push_back(sourceAlignment.strref());
if (failed(emitDebugLine(functionBody, op.getLoc())))
return failure();
encodeInstructionInto(functionBody, spirv::Opcode::OpCopyMemory, operands);
Expand Down
8 changes: 6 additions & 2 deletions mlir/lib/Target/SPIRV/Serialization/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,14 @@ void Serializer::processExtension() {
}

void Serializer::processMemoryModel() {
StringAttr memoryModelName = module.getMemoryModelAttrName();
auto mm = static_cast<uint32_t>(
module->getAttrOfType<spirv::MemoryModelAttr>("memory_model").getValue());
module->getAttrOfType<spirv::MemoryModelAttr>(memoryModelName)
.getValue());

StringAttr addressingModelName = module.getAddressingModelAttrName();
auto am = static_cast<uint32_t>(
module->getAttrOfType<spirv::AddressingModelAttr>("addressing_model")
module->getAttrOfType<spirv::AddressingModelAttr>(addressingModelName)
.getValue());

encodeInstructionInto(memoryModel, spirv::Opcode::OpMemoryModel, {am, mm});
Expand Down