Skip to content

Commit cf9414a

Browse files
committed
simplify translation
1 parent 7e4aeb4 commit cf9414a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/Support/LLVM.h"
1717
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
1818

19+
#include "llvm/ADT/TypeSwitch.h"
1920
#include "llvm/IR/IRBuilder.h"
2021
#include "llvm/IR/InlineAsm.h"
2122
#include "llvm/IR/MDBuilder.h"
@@ -274,16 +275,23 @@ static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder,
274275
LLVM::ModuleTranslation &moduleTranslation) {
275276
llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
276277
for (auto flagAttr : flags.getAsRange<ModuleFlagAttr>()) {
277-
if (auto intAttr = dyn_cast<mlir::IntegerAttr>(flagAttr.getValue()))
278-
llvmModule->addModuleFlag(
279-
convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
280-
flagAttr.getKey().getValue(), intAttr.getUInt());
281-
else if (auto strAttr = dyn_cast<mlir::StringAttr>(flagAttr.getValue())) {
282-
llvmModule->addModuleFlag(
283-
convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
284-
flagAttr.getKey().getValue(),
285-
llvm::MDString::get(builder.getContext(), strAttr.getValue()));
286-
}
278+
llvm::Metadata *valueMetadata =
279+
llvm::TypeSwitch<Attribute, llvm::Metadata *>(flagAttr.getValue())
280+
.Case<StringAttr>([&](auto strAttr) {
281+
return llvm::MDString::get(builder.getContext(),
282+
strAttr.getValue());
283+
})
284+
.Case<IntegerAttr>([&](auto intAttr) {
285+
return llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
286+
llvm::Type::getInt32Ty(builder.getContext()),
287+
intAttr.getInt()));
288+
})
289+
.Default([](auto) { return nullptr; });
290+
291+
assert(metadata && "expected valid metadata");
292+
llvmModule->addModuleFlag(
293+
convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
294+
flagAttr.getKey().getValue(), valueMetadata);
287295
}
288296
}
289297

0 commit comments

Comments
 (0)