Skip to content

Commit

Permalink
[llvm-lib] Handle MIPS architecture (#121007)
Browse files Browse the repository at this point in the history
- add a test to check values for /machine argument
- add a test to check if machine is correctly inferred from inputs
  • Loading branch information
hpoussin authored Dec 27, 2024
1 parent 7deaed9 commit 5d529c3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions llvm/lib/Object/WindowsMachineFlag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ using namespace llvm;

// Returns /machine's value.
COFF::MachineTypes llvm::getMachineType(StringRef S) {
// Flags must be a superset of Microsoft lib.exe /machine flags.
return StringSwitch<COFF::MachineTypes>(S.lower())
.Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64)
.Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386)
.Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT)
.Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64)
.Case("arm64ec", COFF::IMAGE_FILE_MACHINE_ARM64EC)
.Case("arm64x", COFF::IMAGE_FILE_MACHINE_ARM64X)
.Case("mips", COFF::IMAGE_FILE_MACHINE_R4000)
.Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) {
uint16_t Machine = (*Obj)->getMachine();
if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
Machine != COFF::IMAGE_FILE_MACHINE_R4000 &&
Machine != COFF::IMAGE_FILE_MACHINE_ARMNT && !COFF::isAnyArm64(Machine)) {
return createStringError(inconvertibleErrorCode(),
"unknown machine: " + std::to_string(Machine));
Expand All @@ -195,6 +196,8 @@ static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) {
case Triple::aarch64:
return T.isWindowsArm64EC() ? COFF::IMAGE_FILE_MACHINE_ARM64EC
: COFF::IMAGE_FILE_MACHINE_ARM64;
case Triple::mipsel:
return COFF::IMAGE_FILE_MACHINE_R4000;
default:
return createStringError(inconvertibleErrorCode(),
"unknown arch in target triple: " + *TripleStr);
Expand Down
7 changes: 7 additions & 0 deletions llvm/test/tools/llvm-lib/Inputs/mips.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target triple = "mipsel-windows-coff"

; Function Attrs: noinline nounwind optnone
define dso_local void @"?f@@YAXXZ"() #0 {
entry:
ret void
}
19 changes: 19 additions & 0 deletions llvm/test/tools/llvm-lib/infer-machine.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
RUN: rm -rf %t && mkdir -p %t

RUN: llc -mtriple=i386-windows-coff -filetype=obj -o %t/i386.obj %S/Inputs/i386.ll
RUN: llvm-as %S/Inputs/i386.ll -o %t/i386.bc
RUN: llvm-lib %t/i386.obj %t/i386.bc /out:%t/i386.lib
RUN: llvm-objdump -h %t/i386.lib | FileCheck %s --check-prefix=I386
I386: file format coff-i386

RUN: llc -mtriple=x86_64-windows-coff -filetype=obj -o %t/x86_64.obj %S/Inputs/x86_64.ll
RUN: llvm-as %S/Inputs/x86_64.ll -o %t/x86_64.bc
RUN: llvm-lib %t/x86_64.obj %t/x86_64.bc /out:%t/x86_64.lib
RUN: llvm-objdump -h %t/x86_64.lib | FileCheck %s --check-prefix=X86_64
X86_64: file format coff-x86-64

RUN: llc -mtriple=mipsel-windows-coff -filetype=obj -o %t/mips.obj %S/Inputs/mips.ll
RUN: llvm-as %S/Inputs/mips.ll -o %t/mips.bc
RUN: llvm-lib %t/mips.obj %t/mips.bc /out:%t/mips.lib
RUN: llvm-objdump -h %t/mips.lib | FileCheck %s --check-prefix=MIPS
MIPS: file format coff-mips
13 changes: 13 additions & 0 deletions llvm/test/tools/llvm-lib/machine-opt.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RUN: rm -f %t.lib

RUN: llvm-lib /out:%t.lib /machine:i386 2>&1 | FileCheck --check-prefix=EMPTYWARN %s
RUN: llvm-lib /out:%t.lib /machine:amd64 2>&1 | FileCheck --check-prefix=EMPTYWARN %s

RUN: llvm-lib /out:%t.lib /machine:mips 2>&1 | FileCheck --check-prefix=EMPTYWARN %s

RUN: llvm-lib /out:%t.lib /machine:arm 2>&1 | FileCheck --check-prefix=EMPTYWARN %s
RUN: llvm-lib /out:%t.lib /machine:arm64 2>&1 | FileCheck --check-prefix=EMPTYWARN %s
RUN: llvm-lib /out:%t.lib /machine:arm64x 2>&1 | FileCheck --check-prefix=EMPTYWARN %s

EMPTYWARN: warning: no input files, not writing output file

0 comments on commit 5d529c3

Please sign in to comment.