[clang] [llvm] [PowerPC][AIX] Support #pragma comment copyright for AIX (PR #178184)
Tony Varghese via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 13 20:50:00 PDT 2026
================
@@ -0,0 +1,148 @@
+//===-- LowerCommentStringPass.cpp - Lower Comment string metadata -------===//
+//
+// 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
+//
+//===---------------------------------------------------------------------===//
+//
+// LowerCommentStringPass pass lowers module-level comment string metadata
+// emitted by Clang:
+//
+// !comment_string.loadtime = !{!"Copyright ..."}
+//
+// into concrete, translation-unit–local globals.
+// This Pass is enabled only for AIX.
+// For each module (translation unit), the pass performs the following:
+//
+// 1. Creates a null-terminated, internal constant string global
+// (`__loadtime_comment_str`) containing the copyright text in
+// `__loadtime_comment` section.
+//
+// 2. Marks the string in `llvm.used` so it cannot be dropped by
+// optimization or LTO.
+//
+// 3. Attaches `!implicit.ref` metadata referencing the string to every
+// defined function in the module. The PowerPC AIX backend recognizes
+// this metadata and emits a `.ref` directive from the function to the
+// string, creating a concrete relocation that prevents the linker from
+// discarding it (as long as the referencing symbol is kept).
+//
+// Input IR:
+// !comment_string.loadtime = !{!"Copyright"}
+// Output IR:
+// @__loadtime_comment_str = internal constant [N x i8] c"Copyright\00",
+// section "__loadtime_comment"
+// @llvm.used = appending global [1 x ptr] [ptr @__loadtime_comment_str]
+//
+// define i32 @func() !implicit.ref !5 { ... }
+// !5 = !{ptr @__loadtime_comment_str}
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Utils/LowerCommentStringPass.h"
+
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/GlobalValue.h"
+#include "llvm/IR/GlobalVariable.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/TargetParser/Triple.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+
+#define DEBUG_TYPE "lower-comment-string"
+
+using namespace llvm;
+
+static cl::opt<bool>
+ DisableCopyrightMetadata("disable-lower-comment-string", cl::ReallyHidden,
+ cl::desc("Disable LowerCommentString pass."),
+ cl::init(false));
+
+static bool isAIXTriple(const Module &M) {
----------------
tonykuttai wrote:
Agree that it is no needed for this PR. But for easier future integration of Z Os support, I have changed the name to `isSupportedTarget`. We can remove the funciton all together if it is not required.
https://github.com/llvm/llvm-project/pull/178184
More information about the cfe-commits
mailing list