diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp --- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp @@ -8,6 +8,7 @@ #include "DebugTranslation.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/TypeSwitch.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" @@ -51,6 +52,22 @@ // is set explicitly. Windows/MSVC should use CodeView instead. llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1); } + + if (targetTriple.isRISCV()) { + auto dataLayout = llvmModule.getDataLayout(); + llvm::SmallString<7> dataModelStr; + dataModelStr = targetTriple.isRISCV32() ? "ilp32" : "lp64"; + + if (dataLayout.getABITypeAlign(llvm::Type::getFloatTy(llvmCtx)) == 32) { + dataModelStr += 'f'; + } + if (dataLayout.getABITypeAlign(llvm::Type::getDoubleTy(llvmCtx)) == 32) { + dataModelStr += 'd'; + } + + llvmModule.addModuleFlag(llvm::Module::Warning, "target-abi", + llvm::MDString::get(llvmCtx, dataModelStr)); + } } } diff --git a/mlir/test/Dialect/LLVMIR/riscv32-abi.mlir b/mlir/test/Dialect/LLVMIR/riscv32-abi.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/LLVMIR/riscv32-abi.mlir @@ -0,0 +1,11 @@ +// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s + +// CHECK: target triple = "riscv32" +// +// CHECK: llvm.module.flags +// CHECK: !"target-abi", !"ilp32{{.*}}" +module attributes {llvm.target_triple = "riscv32"} { + llvm.func @func() -> () { + llvm.return + } +} diff --git a/mlir/test/Dialect/LLVMIR/riscv64-abi.mlir b/mlir/test/Dialect/LLVMIR/riscv64-abi.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/LLVMIR/riscv64-abi.mlir @@ -0,0 +1,11 @@ +// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s + +// CHECK: target triple = "riscv64" +// +// CHECK: llvm.module.flags +// CHECK: !"target-abi", !"lp64{{.*}}" +module attributes {llvm.target_triple = "riscv64"} { + llvm.func @func() -> () { + llvm.return + } +}