[PATCH] D52290: [driver][mips] Adjust target triple accordingly to provided ABI name

Simon Atanasyan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 20 02:13:38 PDT 2018


atanasyan created this revision.
atanasyan added reviewers: rsmith, rnk, zturner.
Herald added subscribers: jrtc27, arichardson, sdardis.

All MIPS target architectures can be divided into four categories: 32-bit little-endian, 32-bit big-endian, 64-bit little-endian, and 64-bit big-endian. If, for example, default target triple used to build compiler is a little-endian one, but user wants to generate big-endian output it's enough to provide just `-EB` option to implicitly convert default target triple to big-endian variant. But it's impossible to implicitly convert 32-bit target triple to 64-bit variant and vice versa. In other words, if user has Clang built with "mips-linux-gnu" default target triple and wants to generate 64-bit output, he or she has to explicitly provide 64-bit target triple "mips64-linux-gnu".

  clang -target mips64-linux-gnu -c test.c

While gcc in the same case allows to specify just a correct ABI name.

  gcc -mabi=64 -c test.c

The patch solves this problem. If there is no explicit `-target` option, target triple is adjusted accordingly provided ABI name.

For testing this change we need to build Clang with mips default target triple. To catch this case (on MIPS buildbot) and ran a corresponding test case I have to add new "lit" feature `mips-default-target`.


Repository:
  rC Clang

https://reviews.llvm.org/D52290

Files:
  lib/Driver/Driver.cpp
  test/Driver/mips-target-abi.c
  test/lit.cfg.py


Index: test/lit.cfg.py
===================================================================
--- test/lit.cfg.py
+++ test/lit.cfg.py
@@ -136,6 +136,10 @@
 if os.path.exists('/dev/fd/0') and sys.platform not in ['cygwin']:
     config.available_features.add('dev-fd-fs')
 
+# Test that default target triple is mips*
+if re.match(r'^mips*', config.target_triple):
+    config.available_features.add('mips-default-target')
+
 # Not set on native MS environment.
 if not re.match(r'.*-(windows-msvc)$', config.target_triple):
     config.available_features.add('non-ms-sdk')
Index: test/Driver/mips-target-abi.c
===================================================================
--- /dev/null
+++ test/Driver/mips-target-abi.c
@@ -0,0 +1,24 @@
+// Check default target triple adjusting by ABI option.
+//
+// REQUIRES: mips-default-target
+//
+// RUN: %clang -mabi=32 -### %s 2>&1 | FileCheck -check-prefix=O32 %s
+// O32: "-triple" "mips{{[^"]*}}"
+// O32: "-target-cpu" "mips32r2"
+// O32: "-target-abi" "o32"
+// O32: ld{{.*}}"
+// O32: "-m" "elf32btsmip"
+
+// RUN: %clang -mabi=n32 -### %s 2>&1 | FileCheck -check-prefix=N32 %s
+// N32: "-triple" "mips64{{[^"]*}}"
+// N32: "-target-cpu" "mips64r2"
+// N32: "-target-abi" "n32"
+// N32: ld{{.*}}"
+// N32: "-m" "elf32btsmipn32"
+
+// RUN: %clang -mabi=64 -### %s 2>&1 | FileCheck -check-prefix=N64 %s
+// N64: "-triple" "mips64{{[^"]*}}"
+// N64: "-target-cpu" "mips64r2"
+// N64: "-target-abi" "n64"
+// N64: ld{{.*}}"
+// N64: "-m" "elf64btsmip"
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -481,6 +481,17 @@
     Target.setVendorName("intel");
   }
 
+  // If target is MIPS and there is no explicit `-target` option,
+  // adjust the target triple accordingly to provided ABI name.
+  if (Target.isMIPS() && !Args.getLastArg(options::OPT_target)) {
+    if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
+      Target = llvm::StringSwitch<llvm::Triple>(A->getValue())
+                   .Case("32", Target.get32BitArchVariant())
+                   .Case("n32", Target.get64BitArchVariant())
+                   .Case("64", Target.get64BitArchVariant())
+                   .Default(Target);
+  }
+
   return Target;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52290.166237.patch
Type: text/x-patch
Size: 2287 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180920/47195d5f/attachment.bin>


More information about the cfe-commits mailing list