[PATCH] D97109: [clangd] Add support for auxiliary triple specification

Tommy Chiang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 19 21:39:02 PST 2021


oToToT created this revision.
oToToT added reviewers: jdoerfert, sammccall, ilya-biryukov.
oToToT added a project: clang-tools-extra.
Herald added subscribers: usaxena95, kadircet, arphaman.
oToToT requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, MaskRay.
Herald added a project: clang.

I was working on CUDA support for clangd, and I've found that clangd is missing support for auxiliary triple specification. Thus, if we try making clangd work with CUDA or other languages that requires auxiliary triple, it will produce some wrong results.

After some investigation of what clang actually did, I borrowed some code from `clang/lib/Frontend/CompilerInstance.cpp`  to make clangd work as well.

I don't know whether this fix is reasonable or not, but it just fix something looks like a problem in clangd.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97109

Files:
  clang-tools-extra/clangd/Compiler.cpp
  clang/lib/Frontend/PrecompiledPreamble.cpp


Index: clang/lib/Frontend/PrecompiledPreamble.cpp
===================================================================
--- clang/lib/Frontend/PrecompiledPreamble.cpp
+++ clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -370,12 +370,33 @@
   if (!Clang->hasTarget())
     return BuildPreambleError::CouldntCreateTargetInfo;
 
+  // Correctly set AuxTarget. The code is borrowed from
+  // `CompilerInstance::ExecuteAction(FrontendAction &)` inside
+  // "clang/lib/Frontend/CompilerInstance.cpp"
+  if ((Clang->getLangOpts().CUDA || Clang->getLangOpts().OpenMPIsDevice ||
+       Clang->getLangOpts().SYCLIsDevice) &&
+      !Clang->getFrontendOpts().AuxTriple.empty()) {
+    auto TO = std::make_shared<TargetOptions>();
+    TO->Triple = llvm::Triple::normalize(Clang->getFrontendOpts().AuxTriple);
+    if (Clang->getFrontendOpts().AuxTargetCPU)
+      TO->CPU = Clang->getFrontendOpts().AuxTargetCPU.getValue();
+    if (Clang->getFrontendOpts().AuxTargetFeatures)
+      TO->FeaturesAsWritten =
+          Clang->getFrontendOpts().AuxTargetFeatures.getValue();
+    TO->HostTriple = Clang->getTarget().getTriple().str();
+    Clang->setAuxTarget(
+        TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), TO));
+  }
+
   // Inform the target of the language options.
   //
   // FIXME: We shouldn't need to do this, the target should be immutable once
   // created. This complexity should be lifted elsewhere.
   Clang->getTarget().adjust(Clang->getLangOpts());
 
+  if (auto *Aux = Clang->getAuxTarget())
+    Clang->getTarget().setAuxTarget(Aux);
+
   if (Clang->getFrontendOpts().Inputs.size() != 1 ||
       Clang->getFrontendOpts().Inputs[0].getKind().getFormat() !=
           InputKind::Source ||
Index: clang-tools-extra/clangd/Compiler.cpp
===================================================================
--- clang-tools-extra/clangd/Compiler.cpp
+++ clang-tools-extra/clangd/Compiler.cpp
@@ -123,6 +123,33 @@
   if (!Clang->hasTarget())
     return nullptr;
 
+  // Correctly set AuxTarget. The code is borrowed from
+  // `CompilerInstance::ExecuteAction(FrontendAction &)` inside
+  // "clang/lib/Frontend/CompilerInstance.cpp"
+  if ((Clang->getLangOpts().CUDA || Clang->getLangOpts().OpenMPIsDevice ||
+       Clang->getLangOpts().SYCLIsDevice) &&
+      !Clang->getFrontendOpts().AuxTriple.empty()) {
+    auto TO = std::make_shared<TargetOptions>();
+    TO->Triple = llvm::Triple::normalize(Clang->getFrontendOpts().AuxTriple);
+    if (Clang->getFrontendOpts().AuxTargetCPU)
+      TO->CPU = Clang->getFrontendOpts().AuxTargetCPU.getValue();
+    if (Clang->getFrontendOpts().AuxTargetFeatures)
+      TO->FeaturesAsWritten =
+          Clang->getFrontendOpts().AuxTargetFeatures.getValue();
+    TO->HostTriple = Clang->getTarget().getTriple().str();
+    Clang->setAuxTarget(
+        TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), TO));
+  }
+
+  // Inform the target of the language options.
+  //
+  // FIXME: We shouldn't need to do this, the target should be immutable once
+  // created. This complexity should be lifted elsewhere.
+  Clang->getTarget().adjust(Clang->getLangOpts());
+
+  if (auto *Aux = Clang->getAuxTarget())
+    Clang->getTarget().setAuxTarget(Aux);
+
   // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
   // release it.
   Buffer.release();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97109.325150.patch
Type: text/x-patch
Size: 3328 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210220/09fec9a1/attachment-0001.bin>


More information about the cfe-commits mailing list