Skip to content

Commit de8c7e6

Browse files
author
Tony Varghese
committed
Changed the name to comment_string.loadtime
- Fix test failures: Update pipeline tests for CopyrightMetadataPass - Fixed pragma comment copyright test expectations
1 parent 7af5f2d commit de8c7e6

File tree

12 files changed

+48
-50
lines changed

12 files changed

+48
-50
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "clang/Basic/Builtins.h"
4141
#include "clang/Basic/CodeGenOptions.h"
4242
#include "clang/Basic/Diagnostic.h"
43+
// #include "clang/Basic/DiagnosticParse.h"
4344
#include "clang/Basic/Module.h"
4445
#include "clang/Basic/PragmaKinds.h"
4546
#include "clang/Basic/SourceManager.h"
@@ -1557,9 +1558,9 @@ void CodeGenModule::Release() {
15571558
EmitBackendOptionsMetadata(getCodeGenOpts());
15581559

15591560
// Emit copyright metadata
1560-
if (CopyrightCommentInTU) {
1561-
auto *NMD = getModule().getOrInsertNamedMetadata("loadtime.copyright.comment");
1562-
NMD->addOperand(CopyrightCommentInTU);
1561+
if (LoadTimeComment) {
1562+
auto *NMD = getModule().getOrInsertNamedMetadata("comment_string.loadtime");
1563+
NMD->addOperand(LoadTimeComment);
15631564
}
15641565

15651566
// If there is device offloading code embed it in the host now.
@@ -3324,22 +3325,29 @@ void CodeGenModule::AddDependentLib(StringRef Lib) {
33243325
LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
33253326
}
33263327

3327-
/// Process the #pragma comment(copyright, "copyright string ")
3328-
/// and create llvm metadata for the copyright
3328+
/// Process AIX copyright pragma and create LLVM metadata.
3329+
/// #pragma comment(copyright, "string") embed copyright
3330+
/// information into the object file's loader section.
3331+
///
3332+
/// Example: #pragma comment(copyright, "Copyright IBM Corp. 2024")
3333+
///
3334+
/// This should only be called once per translation unit.
33293335
void CodeGenModule::ProcessPragmaComment(PragmaMSCommentKind Kind,
33303336
StringRef Comment) {
3337+
// Ensure we are only processing Copyright Pragmas
3338+
assert(Kind == PCK_Copyright &&
3339+
"Unexpected pragma comment kind, ProcessPragmaComment should only be "
3340+
"called for PCK_Copyright");
33313341

3332-
if (!getTriple().isOSAIX() || Kind != PCK_Copyright)
3342+
// Only one copyright pragma allowed per translation unit
3343+
if (LoadTimeComment) {
33333344
return;
3345+
}
33343346

3335-
assert(
3336-
!CopyrightCommentInTU &&
3337-
"Only one copyright comment should be present in the Translation Unit");
33383347
// Create llvm metadata with the comment string
33393348
auto &C = getLLVMContext();
3340-
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment.str())};
3341-
auto *Node = llvm::MDNode::get(C, Ops);
3342-
CopyrightCommentInTU = Node;
3349+
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment)};
3350+
LoadTimeComment = llvm::MDNode::get(C, Ops);
33433351
}
33443352

33453353
/// Add link options implied by the given module, including modules
@@ -7479,10 +7487,10 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
74797487
AddDependentLib(PCD->getArg());
74807488
break;
74817489
case PCK_Copyright:
7482-
// Skip pragmas deserialized from modules/PCHs
7483-
if (PCD->isFromASTFile())
7484-
break;
7485-
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
7490+
// Skip pragmas deserialized from modules/PCHs. Process the pragma comment
7491+
// only if it originated in this TU and the target OS is AIX.
7492+
if (!PCD->isFromASTFile() && getTriple().isOSAIX())
7493+
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
74867494
break;
74877495
case PCK_Compiler:
74887496
case PCK_ExeStr:

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,8 @@ class CodeGenModule : public CodeGenTypeCache {
587587
/// A vector of metadata strings for dependent libraries for ELF.
588588
SmallVector<llvm::MDNode *, 16> ELFDependentLibraries;
589589

590-
/// Single module-level copyright comment (if any).
591-
/// We only ever accept one per TU.
592-
llvm::MDNode *CopyrightCommentInTU = nullptr;
590+
/// Metadata for copyright pragma comment (if present).
591+
llvm::MDNode *LoadTimeComment = nullptr;
593592

594593
/// @name Cache for Objective-C runtime types
595594
/// @{

clang/lib/Parse/ParsePragma.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,15 +3213,10 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32133213
.Case("copyright", PCK_Copyright)
32143214
.Default(PCK_Unknown);
32153215

3216-
// Restrict copyright to AIX targets only
3217-
if (!PP.getTargetInfo().getTriple().isOSAIX()) {
3218-
switch (Kind) {
3219-
case PCK_Copyright:
3216+
// Restrict copyright to AIX targets only. This could be applied for z/OS
3217+
// and extended with other IBM pragma comment kinds.
3218+
if (!PP.getTargetInfo().getTriple().isOSAIX() && Kind == PCK_Copyright) {
32203219
Kind = PCK_Unknown;
3221-
break;
3222-
default:
3223-
break;
3224-
}
32253220
}
32263221

32273222
if (Kind == PCK_Unknown) {
@@ -3237,6 +3232,8 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32373232

32383233
// On AIX, pragma comment copyright can each appear only once in a TU.
32393234
if (Kind == PCK_Copyright) {
3235+
assert(PP.getTargetInfo().getTriple().isOSAIX() &&
3236+
"Pragma Comment Copyright is supported only on AIX");
32403237
if (SeenCopyrightInTU) {
32413238
PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_once)
32423239
<< II->getName();

clang/test/CodeGen/PowerPC/pragma-comment-copyright-aix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
int main() { return 0; }
2121

2222
// Check that both metadata sections are present
23-
// CHECK: !loadtime.copyright.comment = !{![[copyright:[0-9]+]]}
23+
// CHECK: !comment_string.loadtime = !{![[copyright:[0-9]+]]}
2424

2525
// Check individual metadata content
2626
// CHECK: ![[copyright]] = !{!"@(#) Copyright"}

clang/test/CodeGen/lto-newpm-pipeline.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// CHECK-FULL-O0: Running pass: VerifierPass
2929
// CHECK-FULL-O0-NEXT: Running analysis: VerifierAnalysis
30+
// CHECK-FULL-O0-NEXT: Running pass: CopyrightMetadataPass
3031
// CHECK-FULL-O0-NEXT: Running analysis: InnerAnalysisManagerProxy
3132
// CHECK-FULL-O0-NEXT: Running pass: EntryExitInstrumenterPass
3233
// CHECK-FULL-O0-NEXT: Running pass: AlwaysInlinerPass
@@ -41,6 +42,7 @@
4142

4243
// CHECK-THIN-O0: Running pass: VerifierPass
4344
// CHECK-THIN-O0-NEXT: Running analysis: VerifierAnalysis
45+
// CHECK-THIN-O0-NEXT: Running pass: CopyrightMetadataPass
4446
// CHECK-THIN-O0-NEXT: Running analysis: InnerAnalysisManagerProxy
4547
// CHECK-THIN-O0-NEXT: Running pass: EntryExitInstrumenterPass
4648
// CHECK-THIN-O0-NEXT: Running pass: AlwaysInlinerPass

llvm/lib/Transforms/Utils/CopyrightMetadataPass.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99
// CopyrightMetadataPass pass lowers module-level copyright metadata emitted by
1010
// Clang:
1111
//
12-
// !loadtime.copyright.comment = !{!"Copyright ..."}
13-
//
14-
// into concrete, translation-unit–local globals to ensure that copyright
15-
// strings:
16-
// - survive all optimization and LTO pipelines,
17-
// - are not removed by linker garbage collection, and
18-
// - remain visible in the final XCOFF binary.
12+
// !comment_string.loadtime = !{!"Copyright ..."}
1913
//
14+
// into concrete, translation-unit–local globals.
15+
// This Pass is enabled only for AIX.
2016
// For each module (translation unit), the pass performs the following:
2117
//
2218
// 1. Creates a null-terminated, internal constant string global
@@ -33,7 +29,7 @@
3329
// discarding it (as long as the referencing symbol is kept).
3430
//
3531
// Input IR:
36-
// !loadtime.copyright.comment = !{!"Copyright"}
32+
// !comment_string.loadtime = !{!"Copyright"}
3733
// Output IR:
3834
// @__loadtime_copyright_str = internal constant [N x i8] c"Copyright\00",
3935
// section "__copyright_comment"
@@ -42,11 +38,6 @@
4238
// define i32 @func() !implicit.ref !5 { ... }
4339
// !5 = !{ptr @__loadtime_copyright_str}
4440
//
45-
// The copyright string is placed in the "__copyright_comment" section (mapped to
46-
// an XCOFF csect with [RO] storage class), making it easily identifiable in
47-
// object files and executables. The R_REF relocation prevents the linker
48-
// from discarding this section during garbage collection. Copyright string (if
49-
// kept by the linker) is expected to be loaded at run time.
5041
//===----------------------------------------------------------------------===//
5142

5243
#include "llvm/Transforms/Utils/CopyrightMetadataPass.h"
@@ -90,9 +81,9 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
9081

9182
LLVMContext &Ctx = M.getContext();
9283

93-
// Single-metadata: !loadtime.copyright.comment = !{!0}
84+
// Single-metadata: !comment_string.loadtime = !{!0}
9485
// Each operand node is expected to have one MDString operand.
95-
NamedMDNode *MD = M.getNamedMetadata("loadtime.copyright.comment");
86+
NamedMDNode *MD = M.getNamedMetadata("comment_string.loadtime");
9687
if (!MD || MD->getNumOperands() == 0)
9788
return PreservedAnalyses::all();
9889

@@ -113,7 +104,7 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
113104
// 1. Create a single NULL-terminated string global
114105
Constant *StrInit = ConstantDataArray::getString(Ctx, Text, /*AddNull=*/true);
115106

116-
// Internal, constant, TU-localavoids duplicate symbol issues across TUs.
107+
// Internal, constant, TU-local--avoids duplicate symbol issues across TUs.
117108
auto *StrGV = new GlobalVariable(M, StrInit->getType(),
118109
/*isConstant=*/true,
119110
GlobalValue::InternalLinkage, StrInit,
@@ -122,9 +113,7 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
122113
StrGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
123114
StrGV->setAlignment(Align(1));
124115
// Place in the "__copyright_comment" section.
125-
// Backend maps this to an appropriate XCOFF csect (typically [RO])
126-
// The section will appear in assembly as:
127-
// .csect __copyright_comment[RO],2
116+
// The GV is constant, so we expect a read-only section.
128117
StrGV->setSection("__copyright_comment");
129118

130119
// 2. Add the string to llvm.used to prevent LLVM optimization/LTO passes from
@@ -138,7 +127,6 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
138127
MDNode *ImplicitRefMD = MDNode::get(Ctx, Ops);
139128

140129
// Lambda to attach implicit.ref metadata to a function.
141-
// The backend will translate this into .ref assembly directives.
142130
auto AddImplicitRef = [&](Function &F) {
143131
if (F.isDeclaration())
144132
return;

llvm/test/CodeGen/AArch64/print-pipeline-passes.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt -mtriple=aarch64 -S -passes='default<O2>' -print-pipeline-passes < %s | FileCheck %s
33

44
; CHECK: loop-idiom-vectorize
5-
; O0: {{^}}function(ee-instrument<>),always-inline,coro-cond(coro-early,cgscc(coro-split),coro-cleanup,globaldce),function(annotation-remarks),verify,print{{$}}
5+
; O0: {{^}}copyright-metadata,function(ee-instrument<>),always-inline,coro-cond(coro-early,cgscc(coro-split),coro-cleanup,globaldce),function(annotation-remarks),verify,print{{$}}
66

77
define void @foo() {
88
entry:

llvm/test/Other/new-pm-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
; CHECK-O-NEXT: Running pass: CoroCleanupPass
240240
; CHECK-O-NEXT: Running pass: GlobalOptPass
241241
; CHECK-O-NEXT: Running pass: GlobalDCEPass
242+
; CHECK-O-NEXT: Running pass: CopyrightMetadataPass
242243
; CHECK-DEFAULT-NEXT: Running pass: EliminateAvailableExternallyPass
243244
; CHECK-LTO-NOT: Running pass: EliminateAvailableExternallyPass
244245
; CHECK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass

llvm/test/Other/new-pm-thinlto-postlink-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
; CHECK-O-NEXT: Running pass: CoroCleanupPass
164164
; CHECK-POSTLINK-O-NEXT: Running pass: GlobalOptPass
165165
; CHECK-POSTLINK-O-NEXT: Running pass: GlobalDCEPass
166+
; CHECK-POSTLINK-O-NEXT: Running pass: CopyrightMetadataPass
166167
; CHECK-POSTLINK-O-NEXT: Running pass: EliminateAvailableExternallyPass
167168
; CHECK-POSTLINK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
168169
; CHECK-POSTLINK-O-NEXT: Running pass: RecomputeGlobalsAAPass

llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
; CHECK-O-NEXT: Running pass: CoroCleanupPass
148148
; CHECK-O-NEXT: Running pass: GlobalOptPass
149149
; CHECK-O-NEXT: Running pass: GlobalDCEPass
150+
; CHECK-O-NEXT: Running pass: CopyrightMetadataPass
150151
; CHECK-O-NEXT: Running pass: EliminateAvailableExternallyPass
151152
; CHECK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
152153
; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass

0 commit comments

Comments
 (0)