Skip to content

Commit ff0c347

Browse files
authored
opt: Try to respect target-abi command line option (#169604)
Mips seems kind of broken with these options. n32 seems to override the 64-bit arch with 32-bit pointers, and trying to use any 32-bit mips triple also just errors with any options.
1 parent 75ae0e8 commit ff0c347

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace llvm {
2222

2323
class MCTargetOptions;
2424
enum class EmitDwarfUnwindType;
25+
class StringRef;
2526

2627
namespace mc {
2728

@@ -62,9 +63,9 @@ LLVM_ABI bool getX86RelaxRelocations();
6263

6364
LLVM_ABI bool getX86Sse2Avx();
6465

65-
LLVM_ABI std::string getABIName();
66+
LLVM_ABI StringRef getABIName();
6667

67-
LLVM_ABI std::string getAsSecureLogFile();
68+
LLVM_ABI StringRef getAsSecureLogFile();
6869

6970
/// Create this object with static storage to register mc-related command
7071
/// line options.

llvm/lib/MC/MCTargetOptionsCommandFlags.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ using namespace llvm;
2424
return *NAME##View; \
2525
}
2626

27+
#define MCSTROPT(NAME) \
28+
static cl::opt<std::string> *NAME##View; \
29+
StringRef llvm::mc::get##NAME() { \
30+
assert(NAME##View && "RegisterMCTargetOptionsFlags not created."); \
31+
return *NAME##View; \
32+
}
33+
2734
#define MCOPT_EXP(TY, NAME) \
2835
MCOPT(TY, NAME) \
2936
std::optional<TY> llvm::mc::getExplicit##NAME() { \
@@ -52,8 +59,8 @@ MCOPT(bool, Crel)
5259
MCOPT(bool, ImplicitMapSyms)
5360
MCOPT(bool, X86RelaxRelocations)
5461
MCOPT(bool, X86Sse2Avx)
55-
MCOPT(std::string, ABIName)
56-
MCOPT(std::string, AsSecureLogFile)
62+
MCSTROPT(ABIName)
63+
MCSTROPT(AsSecureLogFile)
5764

5865
llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
5966
#define MCBINDOPT(NAME) \
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; REQUIRES: mips-registered-target
2+
;; Check that we infer the correct datalayout from a target triple
3+
; RUN: opt -mtriple=mips64-- -S -passes=no-op-module -target-abi=n32 < %s | FileCheck -check-prefix=N32 %s
4+
; RUN: opt -mtriple=mips64-- -S -passes=no-op-module -target-abi=n64 < %s | FileCheck -check-prefix=N64 %s
5+
6+
target datalayout = ""
7+
8+
; N32: target datalayout = "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
9+
; N64: target datalayout = "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"

llvm/tools/opt/optdriver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/InitializePasses.h"
3838
#include "llvm/LinkAllIR.h"
3939
#include "llvm/LinkAllPasses.h"
40+
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
4041
#include "llvm/MC/TargetRegistry.h"
4142
#include "llvm/Passes/PassPlugin.h"
4243
#include "llvm/Remarks/HotnessThresholdParser.h"
@@ -516,6 +517,8 @@ optMain(int argc, char **argv,
516517

517518
codegen::MaybeEnableStatistics();
518519

520+
StringRef ABIName = mc::getABIName(); // FIXME: Handle module flag.
521+
519522
// Load the input module...
520523
auto SetDataLayout = [&](StringRef IRTriple,
521524
StringRef IRLayout) -> std::optional<std::string> {
@@ -541,7 +544,7 @@ optMain(int argc, char **argv,
541544

542545
Triple TT(TripleStr);
543546

544-
std::string Str = TT.computeDataLayout();
547+
std::string Str = TT.computeDataLayout(ABIName);
545548
if (Str.empty()) {
546549
errs() << argv[0]
547550
<< ": warning: failed to infer data layout from target triple\n";
@@ -677,9 +680,7 @@ optMain(int argc, char **argv,
677680

678681
RTLIB::RuntimeLibcallsInfo RTLCI(ModuleTriple, codegen::getExceptionModel(),
679682
codegen::getFloatABIForCalls(),
680-
codegen::getEABIVersion(),
681-
"", // FIXME: Get ABI name from MCOptions
682-
VecLib);
683+
codegen::getEABIVersion(), ABIName, VecLib);
683684

684685
// The -disable-simplify-libcalls flag actually disables all builtin optzns.
685686
if (DisableSimplifyLibCalls)

0 commit comments

Comments
 (0)