diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h index adfdccdb5ab77..168131b43cca8 100644 --- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h +++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h @@ -22,6 +22,7 @@ namespace llvm { class MCTargetOptions; enum class EmitDwarfUnwindType; +class StringRef; namespace mc { @@ -62,9 +63,9 @@ LLVM_ABI bool getX86RelaxRelocations(); LLVM_ABI bool getX86Sse2Avx(); -LLVM_ABI std::string getABIName(); +LLVM_ABI StringRef getABIName(); -LLVM_ABI std::string getAsSecureLogFile(); +LLVM_ABI StringRef getAsSecureLogFile(); /// Create this object with static storage to register mc-related command /// line options. diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp index ff95ff78fd53a..22494fa11eb2a 100644 --- a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp +++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp @@ -24,6 +24,13 @@ using namespace llvm; return *NAME##View; \ } +#define MCSTROPT(NAME) \ + static cl::opt *NAME##View; \ + StringRef llvm::mc::get##NAME() { \ + assert(NAME##View && "RegisterMCTargetOptionsFlags not created."); \ + return *NAME##View; \ + } + #define MCOPT_EXP(TY, NAME) \ MCOPT(TY, NAME) \ std::optional llvm::mc::getExplicit##NAME() { \ @@ -52,8 +59,8 @@ MCOPT(bool, Crel) MCOPT(bool, ImplicitMapSyms) MCOPT(bool, X86RelaxRelocations) MCOPT(bool, X86Sse2Avx) -MCOPT(std::string, ABIName) -MCOPT(std::string, AsSecureLogFile) +MCSTROPT(ABIName) +MCSTROPT(AsSecureLogFile) llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() { #define MCBINDOPT(NAME) \ diff --git a/llvm/test/tools/opt/infer-data-layout-target-abi.ll b/llvm/test/tools/opt/infer-data-layout-target-abi.ll new file mode 100644 index 0000000000000..45be56393ac11 --- /dev/null +++ b/llvm/test/tools/opt/infer-data-layout-target-abi.ll @@ -0,0 +1,9 @@ +; REQUIRES: mips-registered-target +;; Check that we infer the correct datalayout from a target triple +; RUN: opt -mtriple=mips64-- -S -passes=no-op-module -target-abi=n32 < %s | FileCheck -check-prefix=N32 %s +; RUN: opt -mtriple=mips64-- -S -passes=no-op-module -target-abi=n64 < %s | FileCheck -check-prefix=N64 %s + +target datalayout = "" + +; N32: target datalayout = "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +; N64: target datalayout = "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" diff --git a/llvm/tools/opt/optdriver.cpp b/llvm/tools/opt/optdriver.cpp index 63c47151389b5..f8be9f16aada6 100644 --- a/llvm/tools/opt/optdriver.cpp +++ b/llvm/tools/opt/optdriver.cpp @@ -37,6 +37,7 @@ #include "llvm/InitializePasses.h" #include "llvm/LinkAllIR.h" #include "llvm/LinkAllPasses.h" +#include "llvm/MC/MCTargetOptionsCommandFlags.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Passes/PassPlugin.h" #include "llvm/Remarks/HotnessThresholdParser.h" @@ -516,6 +517,8 @@ optMain(int argc, char **argv, codegen::MaybeEnableStatistics(); + StringRef ABIName = mc::getABIName(); // FIXME: Handle module flag. + // Load the input module... auto SetDataLayout = [&](StringRef IRTriple, StringRef IRLayout) -> std::optional { @@ -541,7 +544,7 @@ optMain(int argc, char **argv, Triple TT(TripleStr); - std::string Str = TT.computeDataLayout(); + std::string Str = TT.computeDataLayout(ABIName); if (Str.empty()) { errs() << argv[0] << ": warning: failed to infer data layout from target triple\n"; @@ -677,9 +680,7 @@ optMain(int argc, char **argv, RTLIB::RuntimeLibcallsInfo RTLCI(ModuleTriple, codegen::getExceptionModel(), codegen::getFloatABIForCalls(), - codegen::getEABIVersion(), - "", // FIXME: Get ABI name from MCOptions - VecLib); + codegen::getEABIVersion(), ABIName, VecLib); // The -disable-simplify-libcalls flag actually disables all builtin optzns. if (DisableSimplifyLibCalls)