diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h @@ -26,11 +26,13 @@ const MCSubtargetInfo &STI; uint8_t OSABI; bool Is64Bit; + const MCTargetOptions &TargetOptions; public: - LoongArchAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit) - : MCAsmBackend(support::little), STI(STI), OSABI(OSABI), - Is64Bit(Is64Bit) {} + LoongArchAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, + const MCTargetOptions &Options) + : MCAsmBackend(support::little), STI(STI), OSABI(OSABI), Is64Bit(Is64Bit), + TargetOptions(Options) {} ~LoongArchAsmBackend() override {} void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, @@ -63,6 +65,7 @@ std::unique_ptr createObjectTargetWriter() const override; + const MCTargetOptions &getTargetOptions() const { return TargetOptions; } }; } // end namespace llvm diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp @@ -202,5 +202,5 @@ const MCTargetOptions &Options) { const Triple &TT = STI.getTargetTriple(); uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS()); - return new LoongArchAsmBackend(STI, OSABI, TT.isArch64Bit()); + return new LoongArchAsmBackend(STI, OSABI, TT.isArch64Bit(), Options); } diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFStreamer.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFStreamer.cpp --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFStreamer.cpp +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFStreamer.cpp @@ -12,6 +12,7 @@ #include "LoongArchELFStreamer.h" #include "LoongArchAsmBackend.h" +#include "LoongArchBaseInfo.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCCodeEmitter.h" @@ -23,9 +24,10 @@ LoongArchTargetELFStreamer::LoongArchTargetELFStreamer( MCStreamer &S, const MCSubtargetInfo &STI) : LoongArchTargetStreamer(S) { - // FIXME: select appropriate ABI. - setTargetABI(STI.getTargetTriple().isArch64Bit() ? LoongArchABI::ABI_LP64D - : LoongArchABI::ABI_ILP32D); + auto &MAB = static_cast( + getStreamer().getAssembler().getBackend()); + setTargetABI(LoongArchABI::computeTargetABI( + STI.getTargetTriple(), MAB.getTargetOptions().getABIName())); } MCELFStreamer &LoongArchTargetELFStreamer::getStreamer() { diff --git a/llvm/test/CodeGen/LoongArch/e_flags.ll b/llvm/test/CodeGen/LoongArch/e_flags.ll --- a/llvm/test/CodeGen/LoongArch/e_flags.ll +++ b/llvm/test/CodeGen/LoongArch/e_flags.ll @@ -1,15 +1,32 @@ ; RUN: llc --mtriple=loongarch32 --filetype=obj %s -o %t-la32 ; RUN: llvm-readelf -h %t-la32 | FileCheck %s --check-prefixes=ILP32,ABI-D --match-full-lines + +; RUN: llc --mtriple=loongarch32 --filetype=obj %s --target-abi=ilp32s -o %t-ilp32s +; RUN: llvm-readelf -h %t-ilp32s | FileCheck %s --check-prefixes=ILP32,ABI-S --match-full-lines + +; RUN: llc --mtriple=loongarch32 --filetype=obj %s --target-abi=ilp32f -o %t-ilp32f +; RUN: llvm-readelf -h %t-ilp32f | FileCheck %s --check-prefixes=ILP32,ABI-F --match-full-lines + +; RUN: llc --mtriple=loongarch32 --filetype=obj %s --target-abi=ilp32d -o %t-ilp32d +; RUN: llvm-readelf -h %t-ilp32d | FileCheck %s --check-prefixes=ILP32,ABI-D --match-full-lines + ; RUN: llc --mtriple=loongarch64 --filetype=obj %s -o %t-la64 ; RUN: llvm-readelf -h %t-la64 | FileCheck %s --check-prefixes=LP64,ABI-D --match-full-lines -;; Note that we have not support the -target-abi option to select specific ABI. -;; See comments in LoongArchELFStreamer.cpp. So here we only check the default behaviour. -;; After -target-abi is supported, we can add more tests. +; RUN: llc --mtriple=loongarch64 --filetype=obj %s --target-abi=lp64s -o %t-lp64s +; RUN: llvm-readelf -h %t-lp64s | FileCheck %s --check-prefixes=LP64,ABI-S --match-full-lines + +; RUN: llc --mtriple=loongarch64 --filetype=obj %s --target-abi=lp64f -o %t-lp64f +; RUN: llvm-readelf -h %t-lp64f | FileCheck %s --check-prefixes=LP64,ABI-F --match-full-lines + +; RUN: llc --mtriple=loongarch64 --filetype=obj %s --target-abi=lp64d -o %t-lp64d +; RUN: llvm-readelf -h %t-lp64d | FileCheck %s --check-prefixes=LP64,ABI-D --match-full-lines ; LP64: Class: ELF64 ; ILP32: Class: ELF32 +; ABI-S: Flags: 0x41, SOFT-FLOAT, OBJ-v1 +; ABI-F: Flags: 0x42, SINGLE-FLOAT, OBJ-v1 ; ABI-D: Flags: 0x43, DOUBLE-FLOAT, OBJ-v1 define void @foo() {