[clang] [llvm] [AIX][TOC] Add -mtocdata/-mno-tocdata options on AIX (PR #67999)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 5 11:57:49 PST 2024


================
@@ -265,6 +269,61 @@ bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable(
   return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true);
 }
 
+void AIXTargetCodeGenInfo::setTargetAttributes(
+    const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (!isa<llvm::GlobalVariable>(GV))
+    return;
+
+  auto *GVar = dyn_cast<llvm::GlobalVariable>(GV);
+  auto GVId = GV->getName();
+
+  // Is this a global variable specified by the user as toc-data?
+  bool UserSpecifiedTOC =
+      llvm::binary_search(M.getCodeGenOpts().TocDataVarsUserSpecified, GVId);
+  // Assumes the same variable cannot be in both TocVarsUserSpecified and
+  // NoTocVars.
+  if (UserSpecifiedTOC ||
+      ((M.getCodeGenOpts().AllTocData) &&
+       !llvm::binary_search(M.getCodeGenOpts().NoTocDataVars, GVId))) {
+    const unsigned long PointerSize =
+        GV->getParent()->getDataLayout().getPointerSizeInBits() / 8;
+    auto *VarD = dyn_cast<VarDecl>(D);
+    assert(VarD && "Invalid declaration of global variable.");
+
+    ASTContext &Context = D->getASTContext();
+    unsigned Alignment = Context.toBits(Context.getDeclAlign(D)) / 8;
+    const auto *Ty = VarD->getType().getTypePtr();
+    const RecordDecl *RDecl =
+        Ty->isRecordType() ? Ty->getAs<RecordType>()->getDecl() : nullptr;
+
+    bool EmitDiagnostic = UserSpecifiedTOC && GV->hasExternalLinkage();
+    auto reportUnsupportedWarning = [&](bool ShouldEmitWarning, StringRef Msg) {
+      if (ShouldEmitWarning)
+        M.getDiags().Report(D->getLocation(), diag::warn_toc_unsupported_type)
+            << GVId << Msg;
+    };
+    if (!Ty || Ty->isIncompleteType()) {
+      reportUnsupportedWarning(EmitDiagnostic, "of incomplete type");
+    } else if (RDecl && RDecl->hasFlexibleArrayMember()) {
+      reportUnsupportedWarning(EmitDiagnostic,
+                               "it contains a flexible array member");
+    } else if (VarD->getTLSKind() != VarDecl::TLS_None) {
+      reportUnsupportedWarning(EmitDiagnostic, "of thread local storage");
+    } else if (PointerSize < Context.getTypeInfo(VarD->getType()).Width / 8) {
+      reportUnsupportedWarning(EmitDiagnostic,
+                               "variable is larger than a pointer");
+    } else if (PointerSize < Alignment) {
+      reportUnsupportedWarning(EmitDiagnostic,
+                               "variable is aligned wider than a pointer");
+    } else if (D->hasAttr<SectionAttr>()) {
+      reportUnsupportedWarning(EmitDiagnostic, "of a section attribute");
+    } else if ((GV->hasExternalLinkage() || M.getCodeGenOpts().AllTocData) &&
+               !GV->hasLocalLinkage()) {
----------------
diggerlin wrote:

 if GV->hasExternalLinkage() is true ,   the !GV->hasLocalLinkage() is true too.

`((GV->hasExternalLinkage() || M.getCodeGenOpts().AllTocData) && !GV->hasLocalLinkage())`
look weirds.   suggest changing to 
`(GV->hasExternalLinkage() || (M.getCodeGenOpts().AllTocData) && !GV->hasLocalLinkage()))` 
 

https://github.com/llvm/llvm-project/pull/67999


More information about the cfe-commits mailing list