[llvm] f759b4e - [AIX] Generate unique module id based on Pid and timestamp

Xiangling Liao via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 13:23:36 PDT 2020


Author: Xiangling Liao
Date: 2020-08-14T16:22:50-04:00
New Revision: f759b4e43be251ca29e4cd9cbf6af2ed08ff8253

URL: https://github.com/llvm/llvm-project/commit/f759b4e43be251ca29e4cd9cbf6af2ed08ff8253
DIFF: https://github.com/llvm/llvm-project/commit/f759b4e43be251ca29e4cd9cbf6af2ed08ff8253.diff

LOG: [AIX] Generate unique module id based on Pid and timestamp

A unique module id, which is a part of sinit and sterm function names, is
necessary to be unique. However, `getUniqueModuleId` will fail if there is
no strong external symbol within a module. We turn to use Pid and timestamp
when this happens.

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

Added: 
    

Modified: 
    llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
    llvm/test/CodeGen/PowerPC/aix-static-init-no-unique-module-id.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
index 8540e89284fe..920d6ae1f0d6 100644
--- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
@@ -64,6 +64,7 @@
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
@@ -154,8 +155,9 @@ class PPCAIXAsmPrinter : public PPCAsmPrinter {
   /// linkage for them in AIX.
   SmallPtrSet<MCSymbol *, 8> ExtSymSDNodeSymbols;
 
-  /// A unique trailing identifier as a part of sinit/sterm functions.
-  std::string GlobalUniqueModuleId;
+  /// A format indicator and unique trailing identifier to form part of the
+  /// sinit/sterm function names.
+  std::string FormatIndicatorAndUniqueModId;
 
   static void ValidateGV(const GlobalVariable *GV);
   // Record a list of GlobalAlias associated with a GlobalObject.
@@ -1859,17 +1861,23 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
       continue;
 
     if (isSpecialLLVMGlobalArrayForStaticInit(&G)) {
-      // Generate a unique module id which is a part of sinit and sterm function
-      // names.
-      if (GlobalUniqueModuleId.empty()) {
-        GlobalUniqueModuleId = getUniqueModuleId(&M);
-        // FIXME: We need to figure out what to hash on or encode into the
-        // unique ID we need.
-        if (GlobalUniqueModuleId.compare("") == 0)
-          llvm::report_fatal_error(
-              "cannot produce a unique identifier for this module based on"
-              " strong external symbols");
-        GlobalUniqueModuleId = GlobalUniqueModuleId.substr(1);
+      // Generate a format indicator and a unique module id to be a part of
+      // the sinit and sterm function names.
+      if (FormatIndicatorAndUniqueModId.empty()) {
+        std::string UniqueModuleId = getUniqueModuleId(&M);
+        if (UniqueModuleId.compare("") != 0)
+          // TODO: Use source file full path to generate the unique module id
+          // and add a format indicator as a part of function name in case we
+          // will support more than one format.
+          FormatIndicatorAndUniqueModId = "clang_" + UniqueModuleId.substr(1);
+        else
+          // Use the Pid and current time as the unique module id when we cannot
+          // generate one based on a module's strong external symbols.
+          // FIXME: Adjust the comment accordingly after we use source file full
+          // path instead.
+          FormatIndicatorAndUniqueModId =
+              "clangPidTime_" + llvm::itostr(sys::Process::getProcessId()) +
+              "_" + llvm::itostr(time(nullptr));
       }
 
       emitSpecialLLVMGlobal(&G);
@@ -1949,7 +1957,7 @@ void PPCAIXAsmPrinter::emitXXStructorList(const DataLayout &DL,
     llvm::GlobalAlias::create(
         GlobalValue::ExternalLinkage,
         (IsCtor ? llvm::Twine("__sinit") : llvm::Twine("__sterm")) +
-            llvm::Twine("80000000_clang_", GlobalUniqueModuleId) +
+            llvm::Twine("80000000_", FormatIndicatorAndUniqueModId) +
             llvm::Twine("_", llvm::utostr(Index++)),
         cast<Function>(S.Func));
   }

diff  --git a/llvm/test/CodeGen/PowerPC/aix-static-init-no-unique-module-id.ll b/llvm/test/CodeGen/PowerPC/aix-static-init-no-unique-module-id.ll
index 48954e6cec9b..ed24821c1abf 100644
--- a/llvm/test/CodeGen/PowerPC/aix-static-init-no-unique-module-id.ll
+++ b/llvm/test/CodeGen/PowerPC/aix-static-init-no-unique-module-id.ll
@@ -1,5 +1,5 @@
-; RUN: not --crash llc -mtriple powerpc-ibm-aix-xcoff -o - %s 2>&1 | FileCheck %s
-; RUN: not --crash llc -mtriple powerpc64-ibm-aix-xcoff -o - %s 2>&1 | FileCheck %s
+; RUN: llc -mtriple powerpc-ibm-aix-xcoff < %s | FileCheck %s
+; RUN: llc -mtriple powerpc64-ibm-aix-xcoff < %s | FileCheck %s
 
 @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @foo, i8* null }]
 
@@ -7,4 +7,18 @@ define internal void @foo() {
   ret void
 }
 
-; CHECK: LLVM ERROR: cannot produce a unique identifier for this module based on strong external symbols
+; FIXME: Adjust the comment after we use source file full path to generate unique
+; module id instead.
+; Use the Pid and timestamp to generate a unique module id when strong external
+; symbols are not available in current module. The module id generated in this
+; way is not reproducible. A function name sample would be:
+; __sinit80000000_clangPidTime_119189_1597348415_0
+
+; CHECK:              .lglobl        foo[DS]
+; CHECK:              .lglobl        .foo
+; CHECK:              .csect foo[DS]
+; CHECK-NEXT: __sinit80000000_clangPidTime_[[PID:[0-9]+]]_[[TIMESTAMP:[0-9]+]]_0:
+; CHECK:      .foo:
+; CHECK-NEXT: .__sinit80000000_clangPidTime_[[PID]]_[[TIMESTAMP]]_0:
+; CHECK:      .globl	__sinit80000000_clangPidTime_[[PID]]_[[TIMESTAMP]]_0
+; CHECK:      .globl	.__sinit80000000_clangPidTime_[[PID]]_[[TIMESTAMP]]_0


        


More information about the llvm-commits mailing list