]> granicus.if.org Git - llvm/commitdiff
Add AutoUpgrade function to add new address space datalayout string to existing datal...
authorAmy Huang <akhuang@google.com>
Wed, 18 Sep 2019 22:15:58 +0000 (22:15 +0000)
committerAmy Huang <akhuang@google.com>
Wed, 18 Sep 2019 22:15:58 +0000 (22:15 +0000)
Summary:
Add function to AutoUpgrade to change the datalayout of old X86 datalayout strings.
This adds "-p270:32:32-p271:32:32-p272:64:64" to X86 datalayouts that are otherwise valid
and don't already contain it.

This also removes the compatibility changes in https://reviews.llvm.org/D66843.
Datalayout change in https://reviews.llvm.org/D64931.

Reviewers: rnk, echristo

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D67631

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372267 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/AutoUpgrade.h
include/llvm/Target/TargetMachine.h
lib/Bitcode/Reader/BitcodeReader.cpp
lib/IR/AutoUpgrade.cpp
lib/Target/X86/X86TargetMachine.cpp
lib/Target/X86/X86TargetMachine.h
test/Bitcode/upgrade-datalayout.ll [new file with mode: 0644]
test/Bitcode/upgrade-datalayout2.ll [new file with mode: 0644]
test/Bitcode/upgrade-datalayout3.ll [new file with mode: 0644]
unittests/Bitcode/CMakeLists.txt
unittests/Bitcode/DataLayoutUpgradeTest.cpp [new file with mode: 0644]

index 99ab68977a4f8d4a797dd82b7d8d422a3c696dbb..66f38e5b55d106eacc4ba5521a83cfed1ec9ecb7 100644 (file)
@@ -87,6 +87,10 @@ namespace llvm {
   /// Upgrade the loop attachment metadata node.
   MDNode *upgradeInstructionLoopAttachment(MDNode &N);
 
+  /// Upgrade the datalayout string by adding a section for address space
+  /// pointers.
+  std::string UpgradeDataLayoutString(StringRef DL, StringRef Triple);
+
 } // End llvm namespace
 
 #endif
index 0bf5e30afaeae9a5f1cf90d0cef7435628edb8fe..cdf9f8bfd5eaae6fb874473276c9c3d39863f6a8 100644 (file)
@@ -157,7 +157,7 @@ public:
   /// The LLVM Module owns a DataLayout that is used for the target independent
   /// optimizations and code generation. This hook provides a target specific
   /// check on the validity of this DataLayout.
-  virtual bool isCompatibleDataLayout(const DataLayout &Candidate) const {
+  bool isCompatibleDataLayout(const DataLayout &Candidate) const {
     return DL == Candidate;
   }
 
index af906071225bfca875c60fe08511aca4692b5209..89a5b7bdd5b0674d50002d229b758436f826dacf 100644 (file)
@@ -3648,6 +3648,11 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
       break;
     }
     Record.clear();
+
+    // Upgrade data layout string.
+    std::string DL = llvm::UpgradeDataLayoutString(
+        TheModule->getDataLayoutStr(), TheModule->getTargetTriple());
+    TheModule->setDataLayout(DL);
   }
 }
 
index e9d49d8a2b5cb6882f767a95cf2909fc97c3dcbb..e692201dce6c9e6186569dc79c8a9f88d41ebc27 100644 (file)
@@ -4113,3 +4113,23 @@ MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
 
   return MDTuple::get(T->getContext(), Ops);
 }
+
+std::string llvm::UpgradeDataLayoutString(StringRef DL, StringRef TT) {
+  std::string AddrSpaces = "-p270:32:32-p271:32:32-p272:64:64";
+
+  // If X86, and the datalayout matches the expected format, add pointer size
+  // address spaces to the datalayout.
+  Triple::ArchType Arch = Triple(TT).getArch();
+  if ((Arch != llvm::Triple::x86 && Arch != llvm::Triple::x86_64) ||
+      DL.contains(AddrSpaces))
+    return DL;
+
+  SmallVector<StringRef, 4> Groups;
+  Regex R("(e-m:[a-z](-p:32:32)?)(-[if]64:.*$)");
+  if (!R.match(DL, &Groups))
+    return DL;
+
+  SmallString<1024> Buf;
+  std::string Res = (Groups[1] + AddrSpaces + Groups[3]).toStringRef(Buf).str();
+  return Res;
+}
index 8f3d18707619ccba5a43ca27dcfb68602f682e7b..9b74a593b182dd53705b317d7f34891b960313fc 100644 (file)
@@ -106,8 +106,7 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
   llvm_unreachable("unknown subtarget type");
 }
 
-static std::string computeDataLayout(const Triple &TT,
-                                     bool AddressSpaces = true) {
+static std::string computeDataLayout(const Triple &TT) {
   // X86 is little endian
   std::string Ret = "e";
 
@@ -119,8 +118,7 @@ static std::string computeDataLayout(const Triple &TT,
     Ret += "-p:32:32";
 
   // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
-  if (AddressSpaces)
-    Ret += "-p270:32:32-p271:32:32-p272:64:64";
+  Ret += "-p270:32:32-p271:32:32-p272:64:64";
 
   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
   if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
@@ -223,8 +221,7 @@ X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
           getEffectiveRelocModel(TT, JIT, RM),
           getEffectiveX86CodeModel(CM, JIT, TT.getArch() == Triple::x86_64),
           OL),
-      TLOF(createTLOF(getTargetTriple())),
-      DLNoAddrSpaces(computeDataLayout(TT, /*AddressSpaces=*/false)) {
+      TLOF(createTLOF(getTargetTriple())) {
   // On PS4, the "return address" of a 'noreturn' call must still be within
   // the calling function, and TrapUnreachable is an easy way to get that.
   if (TT.isPS4() || TT.isOSBinFormatMachO()) {
@@ -318,13 +315,6 @@ X86TargetMachine::getSubtargetImpl(const Function &F) const {
   return I.get();
 }
 
-bool X86TargetMachine::isCompatibleDataLayout(
-    const DataLayout &Candidate) const {
-  // Maintain compatibility with datalayouts that don't have address space
-  // pointer sizes.
-  return DL == Candidate || DLNoAddrSpaces == Candidate;
-}
-
 //===----------------------------------------------------------------------===//
 // Command line options for x86
 //===----------------------------------------------------------------------===//
index aa970376b2ab2f067140376f88e0af48f8bfe114..b999e2e86af68b96cfd361af5229ac521b3cc3ba 100644 (file)
@@ -30,7 +30,6 @@ class X86RegisterBankInfo;
 class X86TargetMachine final : public LLVMTargetMachine {
   std::unique_ptr<TargetLoweringObjectFile> TLOF;
   mutable StringMap<std::unique_ptr<X86Subtarget>> SubtargetMap;
-  const DataLayout DLNoAddrSpaces;
 
 public:
   X86TargetMachine(const Target &T, const Triple &TT, StringRef CPU,
@@ -53,8 +52,6 @@ public:
   TargetLoweringObjectFile *getObjFileLowering() const override {
     return TLOF.get();
   }
-
-  bool isCompatibleDataLayout(const DataLayout &Candidate) const override;
 };
 
 } // end namespace llvm
diff --git a/test/Bitcode/upgrade-datalayout.ll b/test/Bitcode/upgrade-datalayout.ll
new file mode 100644 (file)
index 0000000..8c00294
--- /dev/null
@@ -0,0 +1,9 @@
+; Test to make sure datalayout is automatically upgraded.
+;
+; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
diff --git a/test/Bitcode/upgrade-datalayout2.ll b/test/Bitcode/upgrade-datalayout2.ll
new file mode 100644 (file)
index 0000000..21de5b8
--- /dev/null
@@ -0,0 +1,10 @@
+; Test to make sure datalayout is not automatically upgraded if it does not
+; match a possible x86 datalayout.
+;
+; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
diff --git a/test/Bitcode/upgrade-datalayout3.ll b/test/Bitcode/upgrade-datalayout3.ll
new file mode 100644 (file)
index 0000000..526ba60
--- /dev/null
@@ -0,0 +1,8 @@
+; Test to make sure datalayout is automatically upgraded.
+;
+; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s
+
+target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
+target triple = "i686-pc-windows-msvc"
+
+; CHECK: target datalayout = "e-m:w-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:32-n8:16:32-S32"
index 7e9d1bc43fefe6e75c34c0124d412e81e457672b..e329e98226d963d1d9350c50e636d92baf06d274 100644 (file)
@@ -8,4 +8,5 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_unittest(BitcodeTests
   BitReaderTest.cpp
+  DataLayoutUpgradeTest.cpp
   )
diff --git a/unittests/Bitcode/DataLayoutUpgradeTest.cpp b/unittests/Bitcode/DataLayoutUpgradeTest.cpp
new file mode 100644 (file)
index 0000000..c924181
--- /dev/null
@@ -0,0 +1,59 @@
+//===- DataLayoutUpgradeTest.cpp - Tests for DataLayout upgrades ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/AutoUpgrade.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(DataLayoutUpgradeTest, ValidDataLayoutUpgrade) {
+  std::string DL1 =
+      UpgradeDataLayoutString("e-m:e-p:32:32-i64:64-f80:128-n8:16:32:64-S128",
+                              "x86_64-unknown-linux-gnu");
+  std::string DL2 = UpgradeDataLayoutString(
+      "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32", "i686-pc-windows-msvc");
+  std::string DL3 = UpgradeDataLayoutString("e-m:o-i64:64-i128:128-n32:64-S128",
+                                            "x86_64-apple-macosx");
+  EXPECT_EQ(DL1, "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64"
+                 "-f80:128-n8:16:32:64-S128");
+  EXPECT_EQ(DL2, "e-m:w-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64"
+                 "-f80:32-n8:16:32-S32");
+  EXPECT_EQ(DL3, "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128"
+                 "-n32:64-S128");
+}
+
+TEST(DataLayoutUpgradeTest, NoDataLayoutUpgrade) {
+  std::string DL1 = UpgradeDataLayoutString(
+      "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32"
+      "-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
+      "-n8:16:32:64-S128",
+      "x86_64-unknown-linux-gnu");
+  std::string DL2 = UpgradeDataLayoutString("e-p:32:32", "i686-apple-darwin9");
+  std::string DL3 = UpgradeDataLayoutString("e-m:e-i64:64-n32:64",
+                                            "powerpc64le-unknown-linux-gnu");
+  std::string DL4 =
+      UpgradeDataLayoutString("e-m:o-i64:64-i128:128-n32:64-S128", "aarch64--");
+  EXPECT_EQ(DL1, "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64"
+                 "-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64"
+                 "-f80:128:128-n8:16:32:64-S128");
+  EXPECT_EQ(DL2, "e-p:32:32");
+  EXPECT_EQ(DL3, "e-m:e-i64:64-n32:64");
+  EXPECT_EQ(DL4, "e-m:o-i64:64-i128:128-n32:64-S128");
+}
+
+TEST(DataLayoutUpgradeTest, EmptyDataLayout) {
+  std::string DL1 = UpgradeDataLayoutString("", "x86_64-unknown-linux-gnu");
+  std::string DL2 = UpgradeDataLayoutString(
+      "e-m:e-p:32:32-i64:64-f80:128-n8:16:32:64-S128", "");
+  EXPECT_EQ(DL1, "");
+  EXPECT_EQ(DL2, "e-m:e-p:32:32-i64:64-f80:128-n8:16:32:64-S128");
+}
+
+} // end namespace