Skip to content

Commit c21153e

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 696ca9f commit c21153e

File tree

12 files changed

+48
-51
lines changed

12 files changed

+48
-51
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "clang/Basic/Builtins.h"
4141
#include "clang/Basic/CodeGenOptions.h"
4242
#include "clang/Basic/Diagnostic.h"
43-
#include "clang/Basic/DiagnosticFrontend.h"
43+
// #include "clang/Basic/DiagnosticParse.h"
4444
#include "clang/Basic/Module.h"
4545
#include "clang/Basic/PragmaKinds.h"
4646
#include "clang/Basic/SourceManager.h"
@@ -1571,9 +1571,9 @@ void CodeGenModule::Release() {
15711571
EmitBackendOptionsMetadata(getCodeGenOpts());
15721572

15731573
// Emit copyright metadata
1574-
if (CopyrightCommentInTU) {
1575-
auto *NMD = getModule().getOrInsertNamedMetadata("loadtime.copyright.comment");
1576-
NMD->addOperand(CopyrightCommentInTU);
1574+
if (LoadTimeComment) {
1575+
auto *NMD = getModule().getOrInsertNamedMetadata("comment_string.loadtime");
1576+
NMD->addOperand(LoadTimeComment);
15771577
}
15781578

15791579
// If there is device offloading code embed it in the host now.
@@ -3409,22 +3409,29 @@ void CodeGenModule::AddDependentLib(StringRef Lib) {
34093409
LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
34103410
}
34113411

3412-
/// Process the #pragma comment(copyright, "copyright string ")
3413-
/// and create llvm metadata for the copyright
3412+
/// Process AIX copyright pragma and create LLVM metadata.
3413+
/// #pragma comment(copyright, "string") embed copyright
3414+
/// information into the object file's loader section.
3415+
///
3416+
/// Example: #pragma comment(copyright, "Copyright IBM Corp. 2024")
3417+
///
3418+
/// This should only be called once per translation unit.
34143419
void CodeGenModule::ProcessPragmaComment(PragmaMSCommentKind Kind,
34153420
StringRef Comment) {
3421+
// Ensure we are only processing Copyright Pragmas
3422+
assert(Kind == PCK_Copyright &&
3423+
"Unexpected pragma comment kind, ProcessPragmaComment should only be "
3424+
"called for PCK_Copyright");
34163425

3417-
if (!getTriple().isOSAIX() || Kind != PCK_Copyright)
3426+
// Only one copyright pragma allowed per translation unit
3427+
if (LoadTimeComment) {
34183428
return;
3429+
}
34193430

3420-
assert(
3421-
!CopyrightCommentInTU &&
3422-
"Only one copyright comment should be present in the Translation Unit");
34233431
// Create llvm metadata with the comment string
34243432
auto &C = getLLVMContext();
3425-
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment.str())};
3426-
auto *Node = llvm::MDNode::get(C, Ops);
3427-
CopyrightCommentInTU = Node;
3433+
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment)};
3434+
LoadTimeComment = llvm::MDNode::get(C, Ops);
34283435
}
34293436

34303437
/// Add link options implied by the given module, including modules
@@ -7605,10 +7612,10 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
76057612
AddDependentLib(PCD->getArg());
76067613
break;
76077614
case PCK_Copyright:
7608-
// Skip pragmas deserialized from modules/PCHs
7609-
if (PCD->isFromASTFile())
7610-
break;
7611-
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
7615+
// Skip pragmas deserialized from modules/PCHs. Process the pragma comment
7616+
// only if it originated in this TU and the target OS is AIX.
7617+
if (!PCD->isFromASTFile() && getTriple().isOSAIX())
7618+
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
76127619
break;
76137620
case PCK_Compiler:
76147621
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
@@ -3214,15 +3214,10 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32143214
.Case("copyright", PCK_Copyright)
32153215
.Default(PCK_Unknown);
32163216

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

32283223
if (Kind == PCK_Unknown) {
@@ -3238,6 +3233,8 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32383233

32393234
// On AIX, pragma comment copyright can each appear only once in a TU.
32403235
if (Kind == PCK_Copyright) {
3236+
assert(PP.getTargetInfo().getTriple().isOSAIX() &&
3237+
"Pragma Comment Copyright is supported only on AIX");
32413238
if (SeenCopyrightInTU) {
32423239
PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_once)
32433240
<< 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),alloc-token,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
@@ -238,6 +238,7 @@
238238
; CHECK-O-NEXT: Running pass: CoroCleanupPass
239239
; CHECK-O-NEXT: Running pass: GlobalOptPass
240240
; CHECK-O-NEXT: Running pass: GlobalDCEPass
241+
; CHECK-O-NEXT: Running pass: CopyrightMetadataPass
241242
; CHECK-DEFAULT-NEXT: Running pass: EliminateAvailableExternallyPass
242243
; CHECK-LTO-NOT: Running pass: EliminateAvailableExternallyPass
243244
; 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
@@ -162,6 +162,7 @@
162162
; CHECK-O-NEXT: Running pass: CoroCleanupPass
163163
; CHECK-POSTLINK-O-NEXT: Running pass: GlobalOptPass
164164
; CHECK-POSTLINK-O-NEXT: Running pass: GlobalDCEPass
165+
; CHECK-POSTLINK-O-NEXT: Running pass: CopyrightMetadataPass
165166
; CHECK-POSTLINK-O-NEXT: Running pass: EliminateAvailableExternallyPass
166167
; CHECK-POSTLINK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
167168
; 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
@@ -146,6 +146,7 @@
146146
; CHECK-O-NEXT: Running pass: CoroCleanupPass
147147
; CHECK-O-NEXT: Running pass: GlobalOptPass
148148
; CHECK-O-NEXT: Running pass: GlobalDCEPass
149+
; CHECK-O-NEXT: Running pass: CopyrightMetadataPass
149150
; CHECK-O-NEXT: Running pass: EliminateAvailableExternallyPass
150151
; CHECK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
151152
; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass

0 commit comments

Comments
 (0)