rprichard created this revision.
Herald added subscribers: jsji, kosarev, mattd, gchakrabarti, asavonic, 
kerbowa, pengfei, jvesely.
Herald added a project: All.
rprichard requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, jholewinski.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.

Previously, NVPTXTargetInfo constructed a TargetInfo using the
TargetOptions::HostTriple field, but did not initialize the target CPU
or CPU features, which resulted in an inaccurate value for
MaxAtomicInlineWidth on x86_32. MaxAtomicInlineWidth is 64 if the host
TargetInfo is initialized with the +cx8 feature (e.g. 586 and up) but
is 32 otherwise.

Instead, add a setAuxTarget override and defer copying from the host
TargetInfo until CompilerInstance::createTarget calls it.

Change setAuxTarget to pass IntrusiveRefCntPtr<TargetInfo> instead of
a const TargetInfo*. NVPTXTargetInfo needs to retain the host
TargetInfo so that it can check calling conventions.

NVPTXTargetInfo was the only user of the TargetOptions::HostTriple
field, so remove the field.

Also see D29542 <https://reviews.llvm.org/D29542> and D56318 
<https://reviews.llvm.org/D56318>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127267

Files:
  clang/include/clang/Basic/TargetInfo.h
  clang/include/clang/Basic/TargetOptions.h
  clang/lib/Basic/Targets/AMDGPU.cpp
  clang/lib/Basic/Targets/AMDGPU.h
  clang/lib/Basic/Targets/NVPTX.cpp
  clang/lib/Basic/Targets/NVPTX.h
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/CompilerInvocation.cpp

Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -4472,17 +4472,6 @@
     }
   }
 
-  if (LangOpts.CUDA) {
-    // During CUDA device-side compilation, the aux triple is the
-    // triple used for host compilation.
-    if (LangOpts.CUDAIsDevice)
-      Res.getTargetOpts().HostTriple = Res.getFrontendOpts().AuxTriple;
-  }
-
-  // Set the triple of the host for OpenMP device compile.
-  if (LangOpts.OpenMPIsDevice)
-    Res.getTargetOpts().HostTriple = Res.getFrontendOpts().AuxTriple;
-
   ParseCodeGenArgs(Res.getCodeGenOpts(), Args, DashX, Diags, T,
                    Res.getFrontendOpts().OutputFile, LangOpts);
 
Index: clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- clang/lib/Frontend/CompilerInstance.cpp
+++ clang/lib/Frontend/CompilerInstance.cpp
@@ -118,7 +118,6 @@
       TO->CPU = getFrontendOpts().AuxTargetCPU.getValue();
     if (getFrontendOpts().AuxTargetFeatures)
       TO->FeaturesAsWritten = getFrontendOpts().AuxTargetFeatures.getValue();
-    TO->HostTriple = getTarget().getTriple().str();
     setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
   }
 
@@ -149,8 +148,8 @@
   // Adjust target options based on codegen options.
   getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts());
 
-  if (auto *Aux = getAuxTarget())
-    getTarget().setAuxTarget(Aux);
+  if (AuxTarget)
+    getTarget().setAuxTarget(AuxTarget);
 
   return true;
 }
Index: clang/lib/Basic/Targets/NVPTX.h
===================================================================
--- clang/lib/Basic/Targets/NVPTX.h
+++ clang/lib/Basic/Targets/NVPTX.h
@@ -60,12 +60,14 @@
   static const Builtin::Info BuiltinInfo[];
   CudaArch GPU;
   uint32_t PTXVersion;
-  std::unique_ptr<TargetInfo> HostTarget;
+  IntrusiveRefCntPtr<TargetInfo> HostTarget;
 
 public:
   NVPTXTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts,
                   unsigned TargetPointerWidth);
 
+  void setAuxTarget(IntrusiveRefCntPtr<TargetInfo> Aux) override;
+
   void getTargetDefines(const LangOptions &Opts,
                         MacroBuilder &Builder) const override;
 
Index: clang/lib/Basic/Targets/NVPTX.cpp
===================================================================
--- clang/lib/Basic/Targets/NVPTX.cpp
+++ clang/lib/Basic/Targets/NVPTX.cpp
@@ -82,32 +82,28 @@
   else
     resetDataLayout("e-i64:64-i128:128-v16:16-v32:32-n16:32:64");
 
-  // If possible, get a TargetInfo for our host triple, so we can match its
-  // types.
-  llvm::Triple HostTriple(Opts.HostTriple);
-  if (!HostTriple.isNVPTX())
-    HostTarget.reset(AllocateTarget(llvm::Triple(Opts.HostTriple), Opts));
-
-  // If no host target, make some guesses about the data layout and return.
-  if (!HostTarget) {
-    LongWidth = LongAlign = TargetPointerWidth;
-    PointerWidth = PointerAlign = TargetPointerWidth;
-    switch (TargetPointerWidth) {
-    case 32:
-      SizeType = TargetInfo::UnsignedInt;
-      PtrDiffType = TargetInfo::SignedInt;
-      IntPtrType = TargetInfo::SignedInt;
-      break;
-    case 64:
-      SizeType = TargetInfo::UnsignedLong;
-      PtrDiffType = TargetInfo::SignedLong;
-      IntPtrType = TargetInfo::SignedLong;
-      break;
-    default:
-      llvm_unreachable("TargetPointerWidth must be 32 or 64");
-    }
-    return;
+  // Set defaults, which will be overridden if there is a host/aux target.
+  LongWidth = LongAlign = TargetPointerWidth;
+  PointerWidth = PointerAlign = TargetPointerWidth;
+  switch (TargetPointerWidth) {
+  case 32:
+    SizeType = TargetInfo::UnsignedInt;
+    PtrDiffType = TargetInfo::SignedInt;
+    IntPtrType = TargetInfo::SignedInt;
+    break;
+  case 64:
+    SizeType = TargetInfo::UnsignedLong;
+    PtrDiffType = TargetInfo::SignedLong;
+    IntPtrType = TargetInfo::SignedLong;
+    break;
+  default:
+    llvm_unreachable("TargetPointerWidth must be 32 or 64");
   }
+}
+
+void NVPTXTargetInfo::setAuxTarget(IntrusiveRefCntPtr<TargetInfo> Aux) {
+  // Retain the host TargetInfo to use later for checking calling conventions.
+  HostTarget = Aux;
 
   // Copy properties from host target.
   PointerWidth = HostTarget->getPointerWidth(/* AddrSpace = */ 0);
Index: clang/lib/Basic/Targets/AMDGPU.h
===================================================================
--- clang/lib/Basic/Targets/AMDGPU.h
+++ clang/lib/Basic/Targets/AMDGPU.h
@@ -425,7 +425,7 @@
       ? ~0 : 0;
   }
 
-  void setAuxTarget(const TargetInfo *Aux) override;
+  void setAuxTarget(IntrusiveRefCntPtr<TargetInfo> Aux) override;
 
   bool hasBitIntType() const override { return true; }
 
Index: clang/lib/Basic/Targets/AMDGPU.cpp
===================================================================
--- clang/lib/Basic/Targets/AMDGPU.cpp
+++ clang/lib/Basic/Targets/AMDGPU.cpp
@@ -452,7 +452,7 @@
   Builder.defineMacro("__AMDGCN_WAVEFRONT_SIZE", Twine(WavefrontSize));
 }
 
-void AMDGPUTargetInfo::setAuxTarget(const TargetInfo *Aux) {
+void AMDGPUTargetInfo::setAuxTarget(IntrusiveRefCntPtr<TargetInfo> Aux) {
   assert(HalfFormat == Aux->HalfFormat);
   assert(FloatFormat == Aux->FloatFormat);
   assert(DoubleFormat == Aux->DoubleFormat);
@@ -462,7 +462,7 @@
   // supported by AMDGPU. Therefore keep its own format for these two types.
   auto SaveLongDoubleFormat = LongDoubleFormat;
   auto SaveFloat128Format = Float128Format;
-  copyAuxTarget(Aux);
+  copyAuxTarget(Aux.get());
   LongDoubleFormat = SaveLongDoubleFormat;
   Float128Format = SaveFloat128Format;
   // For certain builtin types support on the host target, claim they are
Index: clang/include/clang/Basic/TargetOptions.h
===================================================================
--- clang/include/clang/Basic/TargetOptions.h
+++ clang/include/clang/Basic/TargetOptions.h
@@ -28,10 +28,6 @@
   /// The name of the target triple to compile for.
   std::string Triple;
 
-  /// When compiling for the device side, contains the triple used to compile
-  /// for the host.
-  std::string HostTriple;
-
   /// If given, the name of the target CPU to generate code for.
   std::string CPU;
 
Index: clang/include/clang/Basic/TargetInfo.h
===================================================================
--- clang/include/clang/Basic/TargetInfo.h
+++ clang/include/clang/Basic/TargetInfo.h
@@ -1616,7 +1616,7 @@
   virtual bool validateOpenCLTarget(const LangOptions &Opts,
                                     DiagnosticsEngine &Diags) const;
 
-  virtual void setAuxTarget(const TargetInfo *Aux) {}
+  virtual void setAuxTarget(IntrusiveRefCntPtr<TargetInfo> Aux) {}
 
   /// Whether target allows debuginfo types for decl only variables/functions.
   virtual bool allowDebugInfoForExternalRef() const { return false; }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to