Skip to content

Commit

Permalink
Clean up named attribute construction. NFC.
Browse files Browse the repository at this point in the history
With llvm/llvm-project#123974, we no longer have
to construct a StringAttr for the name and can simplify a bunch of code.

Also avoid some needless memory allocations and needless small vectors
in related code.
  • Loading branch information
kuhar committed Jan 25, 2025
1 parent 6ec861f commit 01a4079
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 144 deletions.
17 changes: 5 additions & 12 deletions compiler/plugins/target/CUDA/CUDATarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,8 @@ class CUDATargetDevice final : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);

SmallVector<NamedAttribute> deviceConfigAttrs;
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);

SmallVector<NamedAttribute> executableConfigAttrs;
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
auto deviceConfigAttr = b.getDictionaryAttr({});
auto executableConfigAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand Down Expand Up @@ -424,16 +420,13 @@ class CUDATargetBackend final : public TargetBackend {
getExecutableTarget(MLIRContext *context) const {
Builder b(context);
SmallVector<NamedAttribute> configItems;
auto addConfig = [&](StringRef name, Attribute value) {
configItems.emplace_back(b.getStringAttr(name), value);
};

if (failed(options.verify(b)))
return nullptr;

if (auto target = GPU::getCUDATargetDetails(
options.clTarget, options.clTargetFeatures, context))
addConfig("iree.gpu.target", target);
options.clTarget, options.clTargetFeatures, context)) {
configItems.emplace_back("iree.gpu.target", target);
}

return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
b.getStringAttr("cuda"), b.getStringAttr("cuda-nvptx-fb"),
Expand Down
12 changes: 3 additions & 9 deletions compiler/plugins/target/MetalSPIRV/MetalSPIRVTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class MetalTargetDevice : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);
SmallVector<NamedAttribute> configItems;

auto configAttr = b.getDictionaryAttr(configItems);
auto configAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand Down Expand Up @@ -97,13 +95,9 @@ class MetalSPIRVTargetBackend : public TargetBackend {
IREE::HAL::ExecutableTargetAttr
getExecutableTarget(MLIRContext *context) const {
Builder b(context);
SmallVector<NamedAttribute> configItems;
auto addConfig = [&](StringRef name, Attribute value) {
configItems.emplace_back(b.getStringAttr(name), value);
};

SmallVector<NamedAttribute, 1> configItems;
if (auto target = GPU::getMetalTargetDetails(context)) {
addConfig("iree.gpu.target", target);
configItems.emplace_back("iree.gpu.target", target);
}

return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
Expand Down
20 changes: 6 additions & 14 deletions compiler/plugins/target/ROCM/ROCMTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ class ROCMTargetBackend final : public TargetBackend {
IREE::HAL::ExecutableTargetAttr
getExecutableTarget(StringRef deviceID, MLIRContext *context) const {
Builder b(context);
SmallVector<NamedAttribute> configItems;
SmallVector<NamedAttribute, 4> configItems;
auto addConfig = [&](StringRef name, Attribute value) {
configItems.emplace_back(b.getStringAttr(name), value);
configItems.emplace_back(name, value);
};

if (failed(options.verify(b))) {
Expand Down Expand Up @@ -840,12 +840,8 @@ class AMDGPUTargetDevice final : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);

SmallVector<NamedAttribute> deviceConfigAttrs;
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);

SmallVector<NamedAttribute> executableConfigAttrs;
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
auto deviceConfigAttr = b.getDictionaryAttr({});
auto executableConfigAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand All @@ -870,12 +866,8 @@ class HIPTargetDevice final : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);

SmallVector<NamedAttribute> deviceConfigAttrs;
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);

SmallVector<NamedAttribute> executableConfigAttrs;
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
auto deviceConfigAttr = b.getDictionaryAttr({});
auto executableConfigAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand Down
13 changes: 4 additions & 9 deletions compiler/plugins/target/VMVX/VMVXTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ static IREE::HAL::ExecutableTargetAttr
getVMVXExecutableTarget(bool enableMicrokernels, MLIRContext *context,
StringRef backend, StringRef format) {
Builder b(context);
SmallVector<NamedAttribute> configItems;

configItems.emplace_back(
b.getStringAttr("ukernels"),
b.getStringAttr(enableMicrokernels ? "all" : "none"));
NamedAttribute configItem(
"ukernels", b.getStringAttr(enableMicrokernels ? "all" : "none"));

return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
b.getStringAttr(backend), b.getStringAttr(format),
b.getDictionaryAttr(configItems));
b.getDictionaryAttr(configItem));
}

class VMVXTargetBackend final : public TargetBackend {
Expand Down Expand Up @@ -200,9 +197,7 @@ class VMVXInlineTargetDevice final : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);
SmallVector<NamedAttribute> configItems;

auto configAttr = b.getDictionaryAttr(configItems);
auto configAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand Down
16 changes: 4 additions & 12 deletions compiler/plugins/target/VulkanSPIRV/VulkanSPIRVTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,8 @@ class VulkanTargetDevice : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);

SmallVector<NamedAttribute> deviceConfigAttrs;
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);

SmallVector<NamedAttribute> executableConfigAttrs;
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
auto deviceConfigAttr = b.getDictionaryAttr({});
auto executableConfigAttr = b.getDictionaryAttr({});

SmallVector<IREE::HAL::ExecutableTargetAttr> executableTargetAttrs;
targetRegistry.getTargetBackend("vulkan-spirv")
Expand Down Expand Up @@ -199,13 +195,9 @@ class VulkanSPIRVTargetBackend : public TargetBackend {
IREE::HAL::ExecutableTargetAttr
getExecutableTarget(MLIRContext *context, bool indirectBindings) const {
Builder b(context);
SmallVector<NamedAttribute> configItems;
auto addConfig = [&](StringRef name, Attribute value) {
configItems.emplace_back(b.getStringAttr(name), value);
};

SmallVector<NamedAttribute, 1> configItems;
if (auto target = GPU::getVulkanTargetDetails(options_.target, context)) {
addConfig("iree.gpu.target", target);
configItems.emplace_back("iree.gpu.target", target);
} else {
emitError(b.getUnknownLoc(), "Unknown Vulkan target '")
<< options_.target << "'";
Expand Down
12 changes: 3 additions & 9 deletions compiler/plugins/target/WebGPUSPIRV/WebGPUSPIRVTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class WebGPUTargetDevice : public TargetDevice {
getDefaultDeviceTarget(MLIRContext *context,
const TargetRegistry &targetRegistry) const override {
Builder b(context);
SmallVector<NamedAttribute> configItems;

auto configAttr = b.getDictionaryAttr(configItems);
auto configAttr = b.getDictionaryAttr({});

// If we had multiple target environments we would generate one target attr
// per environment, with each setting its own environment attribute.
Expand Down Expand Up @@ -87,13 +85,9 @@ class WebGPUSPIRVTargetBackend : public TargetBackend {
IREE::HAL::ExecutableTargetAttr
getExecutableTarget(MLIRContext *context) const {
Builder b(context);
SmallVector<NamedAttribute> configItems;
auto addConfig = [&](StringRef name, Attribute value) {
configItems.emplace_back(b.getStringAttr(name), value);
};

SmallVector<NamedAttribute, 1> configItems;
if (auto target = GPU::getWebGPUTargetDetails(context)) {
addConfig("iree.gpu.target", target);
configItems.emplace_back("iree.gpu.target", target);
}

return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,19 @@ static void populateReflectionAttrs(IREE::ABI::InvocationModel invocationModel,

if (auto reflectionAttr =
exportOp->getAttrOfType<DictionaryAttr>("iree.reflection")) {
attrs.append(reflectionAttr.getValue().begin(),
reflectionAttr.getValue().end());
llvm::append_range(attrs, reflectionAttr.getValue());
}

if (auto abiAttr = exportOp->getAttr("iree.abi")) {
attrs.emplace_back(StringAttr::get(context, "iree.abi"), abiAttr);
attrs.emplace_back("iree.abi", abiAttr);
}

switch (invocationModel) {
default:
case IREE::ABI::InvocationModel::Sync:
break;
case IREE::ABI::InvocationModel::CoarseFences:
attrs.emplace_back(StringAttr::get(context, "iree.abi.model"),
attrs.emplace_back("iree.abi.model",
StringAttr::get(context, "coarse-fences"));
break;
}
Expand All @@ -484,10 +483,9 @@ static void populateReflectionAttrs(IREE::ABI::InvocationModel invocationModel,
// Users in source frontends can override this with something more natural
// (python/whatever).
if (auto declAttr = exportOp->getAttr("iree.abi.declaration")) {
attrs.emplace_back(StringAttr::get(context, "iree.abi.declaration"),
declAttr);
attrs.emplace_back("iree.abi.declaration", declAttr);
} else {
attrs.emplace_back(StringAttr::get(context, "iree.abi.declaration"),
attrs.emplace_back("iree.abi.declaration",
formatSourceDeclaration(invocationModel, exportOp,
wrapperOp.getName(),
exportOp.getAllArgAttrs(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ class WrapEntryPointsPass
// IO tensor names and quantization information.
void populateReflectionAttrs(IREE::Util::FuncOp entryFuncOp,
IREE::Util::FuncOp wrapperFuncOp) {
SmallVector<NamedAttribute> attrs;
SmallVector<NamedAttribute, 1> attrs;
attrs.push_back(buildIONamesAttr(entryFuncOp));
// TODO(#3972): tfl.io.quant: quantization information.
// TODO(#3978): tfl.io.types: tensor types (complex/strings/etc).
Expand Down Expand Up @@ -636,7 +636,7 @@ class WrapEntryPointsPass
}
}
return NamedAttribute{
StringAttr::get(&getContext(), "tfl.io.names"),
"tfl.io.names",
StringAttr::get(&getContext(), llvm::join(pieces, ";"))};
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ DictionaryAttr getLayoutImpl(Attribute attr, RankedTensorType type) {
MLIRContext *ctx = attr.getContext();
auto deviceLayoutAttr = cast<IREE::Codegen::LayoutAttrInterface>(attr);
const MaterializeEncodingInfo info = deviceLayoutAttr.getEncodingInfo(type);
auto strAttr = StringAttr::get(ctx, kEncodingInfoAttrName);
Attribute encodingInfoAttr =
IREE::Codegen::serializeEncodingInfo(attr.getContext(), info);
return DictionaryAttr::get(ctx, {NamedAttribute(strAttr, encodingInfoAttr)});
return DictionaryAttr::get(
ctx, NamedAttribute(kEncodingInfoAttrName, encodingInfoAttr));
}

} // namespace mlir::iree_compiler::IREE
Loading

0 comments on commit 01a4079

Please sign in to comment.