Skip to content

Commit 784104b

Browse files
kuharAWoloszyn
authored andcommitted
Clean up named attribute construction. NFC. (#19813)
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.
1 parent 7e296fb commit 784104b

File tree

13 files changed

+78
-144
lines changed

13 files changed

+78
-144
lines changed

compiler/plugins/target/CUDA/CUDATarget.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,8 @@ class CUDATargetDevice final : public TargetDevice {
385385
getDefaultDeviceTarget(MLIRContext *context,
386386
const TargetRegistry &targetRegistry) const override {
387387
Builder b(context);
388-
389-
SmallVector<NamedAttribute> deviceConfigAttrs;
390-
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);
391-
392-
SmallVector<NamedAttribute> executableConfigAttrs;
393-
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
388+
auto deviceConfigAttr = b.getDictionaryAttr({});
389+
auto executableConfigAttr = b.getDictionaryAttr({});
394390

395391
// If we had multiple target environments we would generate one target attr
396392
// per environment, with each setting its own environment attribute.
@@ -424,16 +420,13 @@ class CUDATargetBackend final : public TargetBackend {
424420
getExecutableTarget(MLIRContext *context) const {
425421
Builder b(context);
426422
SmallVector<NamedAttribute> configItems;
427-
auto addConfig = [&](StringRef name, Attribute value) {
428-
configItems.emplace_back(b.getStringAttr(name), value);
429-
};
430-
431423
if (failed(options.verify(b)))
432424
return nullptr;
433425

434426
if (auto target = GPU::getCUDATargetDetails(
435-
options.clTarget, options.clTargetFeatures, context))
436-
addConfig("iree.gpu.target", target);
427+
options.clTarget, options.clTargetFeatures, context)) {
428+
configItems.emplace_back("iree.gpu.target", target);
429+
}
437430

438431
return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
439432
b.getStringAttr("cuda"), b.getStringAttr("cuda-nvptx-fb"),

compiler/plugins/target/MetalSPIRV/MetalSPIRVTarget.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ class MetalTargetDevice : public TargetDevice {
6161
getDefaultDeviceTarget(MLIRContext *context,
6262
const TargetRegistry &targetRegistry) const override {
6363
Builder b(context);
64-
SmallVector<NamedAttribute> configItems;
65-
66-
auto configAttr = b.getDictionaryAttr(configItems);
64+
auto configAttr = b.getDictionaryAttr({});
6765

6866
// If we had multiple target environments we would generate one target attr
6967
// per environment, with each setting its own environment attribute.
@@ -97,13 +95,9 @@ class MetalSPIRVTargetBackend : public TargetBackend {
9795
IREE::HAL::ExecutableTargetAttr
9896
getExecutableTarget(MLIRContext *context) const {
9997
Builder b(context);
100-
SmallVector<NamedAttribute> configItems;
101-
auto addConfig = [&](StringRef name, Attribute value) {
102-
configItems.emplace_back(b.getStringAttr(name), value);
103-
};
104-
98+
SmallVector<NamedAttribute, 1> configItems;
10599
if (auto target = GPU::getMetalTargetDetails(context)) {
106-
addConfig("iree.gpu.target", target);
100+
configItems.emplace_back("iree.gpu.target", target);
107101
}
108102

109103
return b.getAttr<IREE::HAL::ExecutableTargetAttr>(

compiler/plugins/target/ROCM/ROCMTarget.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ class ROCMTargetBackend final : public TargetBackend {
228228
IREE::HAL::ExecutableTargetAttr
229229
getExecutableTarget(StringRef deviceID, MLIRContext *context) const {
230230
Builder b(context);
231-
SmallVector<NamedAttribute> configItems;
231+
SmallVector<NamedAttribute, 4> configItems;
232232
auto addConfig = [&](StringRef name, Attribute value) {
233-
configItems.emplace_back(b.getStringAttr(name), value);
233+
configItems.emplace_back(name, value);
234234
};
235235

236236
if (failed(options.verify(b))) {
@@ -840,12 +840,8 @@ class AMDGPUTargetDevice final : public TargetDevice {
840840
getDefaultDeviceTarget(MLIRContext *context,
841841
const TargetRegistry &targetRegistry) const override {
842842
Builder b(context);
843-
844-
SmallVector<NamedAttribute> deviceConfigAttrs;
845-
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);
846-
847-
SmallVector<NamedAttribute> executableConfigAttrs;
848-
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
843+
auto deviceConfigAttr = b.getDictionaryAttr({});
844+
auto executableConfigAttr = b.getDictionaryAttr({});
849845

850846
// If we had multiple target environments we would generate one target attr
851847
// per environment, with each setting its own environment attribute.
@@ -870,12 +866,8 @@ class HIPTargetDevice final : public TargetDevice {
870866
getDefaultDeviceTarget(MLIRContext *context,
871867
const TargetRegistry &targetRegistry) const override {
872868
Builder b(context);
873-
874-
SmallVector<NamedAttribute> deviceConfigAttrs;
875-
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);
876-
877-
SmallVector<NamedAttribute> executableConfigAttrs;
878-
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
869+
auto deviceConfigAttr = b.getDictionaryAttr({});
870+
auto executableConfigAttr = b.getDictionaryAttr({});
879871

880872
// If we had multiple target environments we would generate one target attr
881873
// per environment, with each setting its own environment attribute.

compiler/plugins/target/VMVX/VMVXTarget.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,12 @@ static IREE::HAL::ExecutableTargetAttr
4444
getVMVXExecutableTarget(bool enableMicrokernels, MLIRContext *context,
4545
StringRef backend, StringRef format) {
4646
Builder b(context);
47-
SmallVector<NamedAttribute> configItems;
48-
49-
configItems.emplace_back(
50-
b.getStringAttr("ukernels"),
51-
b.getStringAttr(enableMicrokernels ? "all" : "none"));
47+
NamedAttribute configItem(
48+
"ukernels", b.getStringAttr(enableMicrokernels ? "all" : "none"));
5249

5350
return b.getAttr<IREE::HAL::ExecutableTargetAttr>(
5451
b.getStringAttr(backend), b.getStringAttr(format),
55-
b.getDictionaryAttr(configItems));
52+
b.getDictionaryAttr(configItem));
5653
}
5754

5855
class VMVXTargetBackend final : public TargetBackend {
@@ -200,9 +197,7 @@ class VMVXInlineTargetDevice final : public TargetDevice {
200197
getDefaultDeviceTarget(MLIRContext *context,
201198
const TargetRegistry &targetRegistry) const override {
202199
Builder b(context);
203-
SmallVector<NamedAttribute> configItems;
204-
205-
auto configAttr = b.getDictionaryAttr(configItems);
200+
auto configAttr = b.getDictionaryAttr({});
206201

207202
// If we had multiple target environments we would generate one target attr
208203
// per environment, with each setting its own environment attribute.

compiler/plugins/target/VulkanSPIRV/VulkanSPIRVTarget.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,8 @@ class VulkanTargetDevice : public TargetDevice {
160160
getDefaultDeviceTarget(MLIRContext *context,
161161
const TargetRegistry &targetRegistry) const override {
162162
Builder b(context);
163-
164-
SmallVector<NamedAttribute> deviceConfigAttrs;
165-
auto deviceConfigAttr = b.getDictionaryAttr(deviceConfigAttrs);
166-
167-
SmallVector<NamedAttribute> executableConfigAttrs;
168-
auto executableConfigAttr = b.getDictionaryAttr(executableConfigAttrs);
163+
auto deviceConfigAttr = b.getDictionaryAttr({});
164+
auto executableConfigAttr = b.getDictionaryAttr({});
169165

170166
SmallVector<IREE::HAL::ExecutableTargetAttr> executableTargetAttrs;
171167
targetRegistry.getTargetBackend("vulkan-spirv")
@@ -199,13 +195,9 @@ class VulkanSPIRVTargetBackend : public TargetBackend {
199195
IREE::HAL::ExecutableTargetAttr
200196
getExecutableTarget(MLIRContext *context, bool indirectBindings) const {
201197
Builder b(context);
202-
SmallVector<NamedAttribute> configItems;
203-
auto addConfig = [&](StringRef name, Attribute value) {
204-
configItems.emplace_back(b.getStringAttr(name), value);
205-
};
206-
198+
SmallVector<NamedAttribute, 1> configItems;
207199
if (auto target = GPU::getVulkanTargetDetails(options_.target, context)) {
208-
addConfig("iree.gpu.target", target);
200+
configItems.emplace_back("iree.gpu.target", target);
209201
} else {
210202
emitError(b.getUnknownLoc(), "Unknown Vulkan target '")
211203
<< options_.target << "'";

compiler/plugins/target/WebGPUSPIRV/WebGPUSPIRVTarget.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ class WebGPUTargetDevice : public TargetDevice {
5151
getDefaultDeviceTarget(MLIRContext *context,
5252
const TargetRegistry &targetRegistry) const override {
5353
Builder b(context);
54-
SmallVector<NamedAttribute> configItems;
55-
56-
auto configAttr = b.getDictionaryAttr(configItems);
54+
auto configAttr = b.getDictionaryAttr({});
5755

5856
// If we had multiple target environments we would generate one target attr
5957
// per environment, with each setting its own environment attribute.
@@ -87,13 +85,9 @@ class WebGPUSPIRVTargetBackend : public TargetBackend {
8785
IREE::HAL::ExecutableTargetAttr
8886
getExecutableTarget(MLIRContext *context) const {
8987
Builder b(context);
90-
SmallVector<NamedAttribute> configItems;
91-
auto addConfig = [&](StringRef name, Attribute value) {
92-
configItems.emplace_back(b.getStringAttr(name), value);
93-
};
94-
88+
SmallVector<NamedAttribute, 1> configItems;
9589
if (auto target = GPU::getWebGPUTargetDetails(context)) {
96-
addConfig("iree.gpu.target", target);
90+
configItems.emplace_back("iree.gpu.target", target);
9791
}
9892

9993
return b.getAttr<IREE::HAL::ExecutableTargetAttr>(

compiler/src/iree/compiler/Bindings/Native/Transforms/WrapEntryPoints.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,19 @@ static void populateReflectionAttrs(IREE::ABI::InvocationModel invocationModel,
462462

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

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

473472
switch (invocationModel) {
474473
default:
475474
case IREE::ABI::InvocationModel::Sync:
476475
break;
477476
case IREE::ABI::InvocationModel::CoarseFences:
478-
attrs.emplace_back(StringAttr::get(context, "iree.abi.model"),
477+
attrs.emplace_back("iree.abi.model",
479478
StringAttr::get(context, "coarse-fences"));
480479
break;
481480
}
@@ -484,10 +483,9 @@ static void populateReflectionAttrs(IREE::ABI::InvocationModel invocationModel,
484483
// Users in source frontends can override this with something more natural
485484
// (python/whatever).
486485
if (auto declAttr = exportOp->getAttr("iree.abi.declaration")) {
487-
attrs.emplace_back(StringAttr::get(context, "iree.abi.declaration"),
488-
declAttr);
486+
attrs.emplace_back("iree.abi.declaration", declAttr);
489487
} else {
490-
attrs.emplace_back(StringAttr::get(context, "iree.abi.declaration"),
488+
attrs.emplace_back("iree.abi.declaration",
491489
formatSourceDeclaration(invocationModel, exportOp,
492490
wrapperOp.getName(),
493491
exportOp.getAllArgAttrs(),

compiler/src/iree/compiler/Bindings/TFLite/Transforms/WrapEntryPoints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class WrapEntryPointsPass
605605
// IO tensor names and quantization information.
606606
void populateReflectionAttrs(IREE::Util::FuncOp entryFuncOp,
607607
IREE::Util::FuncOp wrapperFuncOp) {
608-
SmallVector<NamedAttribute> attrs;
608+
SmallVector<NamedAttribute, 1> attrs;
609609
attrs.push_back(buildIONamesAttr(entryFuncOp));
610610
// TODO(#3972): tfl.io.quant: quantization information.
611611
// TODO(#3978): tfl.io.types: tensor types (complex/strings/etc).
@@ -636,7 +636,7 @@ class WrapEntryPointsPass
636636
}
637637
}
638638
return NamedAttribute{
639-
StringAttr::get(&getContext(), "tfl.io.names"),
639+
"tfl.io.names",
640640
StringAttr::get(&getContext(), llvm::join(pieces, ";"))};
641641
}
642642
};

compiler/src/iree/compiler/Codegen/ExternalInterfaces/Utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ DictionaryAttr getLayoutImpl(Attribute attr, RankedTensorType type) {
9696
MLIRContext *ctx = attr.getContext();
9797
auto deviceLayoutAttr = cast<IREE::Codegen::LayoutAttrInterface>(attr);
9898
const MaterializeEncodingInfo info = deviceLayoutAttr.getEncodingInfo(type);
99-
auto strAttr = StringAttr::get(ctx, kEncodingInfoAttrName);
10099
Attribute encodingInfoAttr =
101100
IREE::Codegen::serializeEncodingInfo(attr.getContext(), info);
102-
return DictionaryAttr::get(ctx, {NamedAttribute(strAttr, encodingInfoAttr)});
101+
return DictionaryAttr::get(
102+
ctx, NamedAttribute(kEncodingInfoAttrName, encodingInfoAttr));
103103
}
104104

105105
} // namespace mlir::iree_compiler::IREE

0 commit comments

Comments
 (0)