Skip to content

Commit 7af5f2d

Browse files
author
Tony Varghese
committed
Chnaged to platform agnostic names for llvm metadata and variables used
1 parent 82254ee commit 7af5f2d

File tree

7 files changed

+75
-121
lines changed

7 files changed

+75
-121
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "clang/Basic/CodeGenOptions.h"
4242
#include "clang/Basic/Diagnostic.h"
4343
#include "clang/Basic/Module.h"
44+
#include "clang/Basic/PragmaKinds.h"
4445
#include "clang/Basic/SourceManager.h"
4546
#include "clang/Basic/TargetInfo.h"
4647
#include "clang/Basic/Version.h"
@@ -1555,10 +1556,10 @@ void CodeGenModule::Release() {
15551556

15561557
EmitBackendOptionsMetadata(getCodeGenOpts());
15571558

1558-
// Emit copyright metadata for AIX
1559-
if (AIXCopyrightComment) {
1560-
auto *NMD = getModule().getOrInsertNamedMetadata("aix.copyright.comment");
1561-
NMD->addOperand(AIXCopyrightComment);
1559+
// Emit copyright metadata
1560+
if (CopyrightCommentInTU) {
1561+
auto *NMD = getModule().getOrInsertNamedMetadata("loadtime.copyright.comment");
1562+
NMD->addOperand(CopyrightCommentInTU);
15621563
}
15631564

15641565
// If there is device offloading code embed it in the host now.
@@ -3325,17 +3326,20 @@ void CodeGenModule::AddDependentLib(StringRef Lib) {
33253326

33263327
/// Process the #pragma comment(copyright, "copyright string ")
33273328
/// and create llvm metadata for the copyright
3328-
void CodeGenModule::ProcessPragmaCommentCopyright(StringRef Comment) {
3329+
void CodeGenModule::ProcessPragmaComment(PragmaMSCommentKind Kind,
3330+
StringRef Comment) {
33293331

3330-
if (!getTriple().isOSAIX())
3332+
if (!getTriple().isOSAIX() || Kind != PCK_Copyright)
33313333
return;
33323334

3335+
assert(
3336+
!CopyrightCommentInTU &&
3337+
"Only one copyright comment should be present in the Translation Unit");
33333338
// Create llvm metadata with the comment string
33343339
auto &C = getLLVMContext();
33353340
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment.str())};
33363341
auto *Node = llvm::MDNode::get(C, Ops);
3337-
if(!AIXCopyrightComment)
3338-
AIXCopyrightComment = Node;
3342+
CopyrightCommentInTU = Node;
33393343
}
33403344

33413345
/// Add link options implied by the given module, including modules
@@ -7478,7 +7482,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
74787482
// Skip pragmas deserialized from modules/PCHs
74797483
if (PCD->isFromASTFile())
74807484
break;
7481-
ProcessPragmaCommentCopyright(PCD->getArg());
7485+
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
74827486
break;
74837487
case PCK_Compiler:
74847488
case PCK_ExeStr:

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ 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 for AIX (if any).
590+
/// Single module-level copyright comment (if any).
591591
/// We only ever accept one per TU.
592-
llvm::MDNode *AIXCopyrightComment = nullptr;
592+
llvm::MDNode *CopyrightCommentInTU = nullptr;
593593

594594
/// @name Cache for Objective-C runtime types
595595
/// @{
@@ -1462,8 +1462,8 @@ class CodeGenModule : public CodeGenTypeCache {
14621462
/// Appends a dependent lib to the appropriate metadata value.
14631463
void AddDependentLib(StringRef Lib);
14641464

1465-
/// Record the AIX copyright comment in module-level metadata.
1466-
void ProcessPragmaCommentCopyright(StringRef Comment);
1465+
/// Process pragma comment
1466+
void ProcessPragmaComment(PragmaMSCommentKind Kind, StringRef Comment);
14671467

14681468
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
14691469

clang/lib/Parse/ParsePragma.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct PragmaCommentHandler : public PragmaHandler {
236236

237237
private:
238238
Sema &Actions;
239-
bool SeenAIXCopyright = false; // TU-scoped
239+
bool SeenCopyrightInTU = false; // TU-scoped
240240
};
241241

242242
struct PragmaDetectMismatchHandler : public PragmaHandler {
@@ -3204,14 +3204,26 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32043204
// Verify that this is one of the 5 explicitly listed options.
32053205
IdentifierInfo *II = Tok.getIdentifierInfo();
32063206
PragmaMSCommentKind Kind =
3207-
llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
3208-
.Case("linker", PCK_Linker)
3209-
.Case("lib", PCK_Lib)
3210-
.Case("compiler", PCK_Compiler)
3211-
.Case("exestr", PCK_ExeStr)
3212-
.Case("user", PCK_User)
3213-
.Case("copyright", PCK_Copyright)
3214-
.Default(PCK_Unknown);
3207+
llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
3208+
.Case("linker", PCK_Linker)
3209+
.Case("lib", PCK_Lib)
3210+
.Case("compiler", PCK_Compiler)
3211+
.Case("exestr", PCK_ExeStr)
3212+
.Case("user", PCK_User)
3213+
.Case("copyright", PCK_Copyright)
3214+
.Default(PCK_Unknown);
3215+
3216+
// Restrict copyright to AIX targets only
3217+
if (!PP.getTargetInfo().getTriple().isOSAIX()) {
3218+
switch (Kind) {
3219+
case PCK_Copyright:
3220+
Kind = PCK_Unknown;
3221+
break;
3222+
default:
3223+
break;
3224+
}
3225+
}
3226+
32153227
if (Kind == PCK_Unknown) {
32163228
PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
32173229
return;
@@ -3224,13 +3236,13 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32243236
}
32253237

32263238
// On AIX, pragma comment copyright can each appear only once in a TU.
3227-
if (PP.getTargetInfo().getTriple().isOSAIX() && Kind == PCK_Copyright) {
3228-
if (SeenAIXCopyright) {
3229-
PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_once)
3230-
<< II->getName();
3231-
return;
3232-
}
3233-
SeenAIXCopyright = true;
3239+
if (Kind == PCK_Copyright) {
3240+
if (SeenCopyrightInTU) {
3241+
PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_once)
3242+
<< II->getName();
3243+
return;
3244+
}
3245+
SeenCopyrightInTU = true;
32343246
}
32353247

32363248
// Read the optional string if present.
@@ -3259,11 +3271,9 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32593271
return;
32603272
}
32613273

3262-
if (PP.getTargetInfo().getTriple().isOSAIX()) {
3263-
// Accept and ignore well-formed copyright with empty string.
3264-
if(Kind == PCK_Copyright && ArgumentString.empty())
3265-
return;
3266-
}
3274+
// Accept and ignore well-formed copyright with empty string.
3275+
if (Kind == PCK_Copyright && ArgumentString.empty())
3276+
return;
32673277

32683278
// If the pragma is lexically sound, notify any interested PPCallbacks.
32693279
if (PP.getPPCallbacks())

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: !aix.copyright.comment = !{![[copyright:[0-9]+]]}
23+
// CHECK: !loadtime.copyright.comment = !{![[copyright:[0-9]+]]}
2424

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

llvm/lib/Transforms/Utils/CopyrightMetadataPass.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
//===-- CopyrightMetadataPass.cpp - Lower AIX copyright metadata ----------===//
1+
//===-- CopyrightMetadataPass.cpp - Lower copyright metadata -------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7-
//===----------------------------------------------------------------------===//
7+
//===---------------------------------------------------------------------===//
88
//
99
// CopyrightMetadataPass pass lowers module-level copyright metadata emitted by
1010
// Clang:
1111
//
12-
// !aix.copyright.comment = !{!"Copyright ..."}
12+
// !loadtime.copyright.comment = !{!"Copyright ..."}
1313
//
1414
// into concrete, translation-unit–local globals to ensure that copyright
1515
// strings:
@@ -20,8 +20,8 @@
2020
// For each module (translation unit), the pass performs the following:
2121
//
2222
// 1. Creates a null-terminated, internal constant string global
23-
// (`__aix_copyright_str`) containing the copyright text in
24-
// `__aix_copyright` section..
23+
// (`__loadtime_copyright_str`) containing the copyright text in
24+
// `__copyright_comment` section.
2525
//
2626
// 2. Marks the string in `llvm.used` so it cannot be dropped by
2727
// optimization or LTO.
@@ -33,16 +33,16 @@
3333
// discarding it (as long as the referencing symbol is kept).
3434
//
3535
// Input IR:
36-
// !aix.copyright.comment = !{!"Copyright"}
36+
// !loadtime.copyright.comment = !{!"Copyright"}
3737
// Output IR:
38-
// @__aix_copyright_str = internal constant [N x i8] c"Copyright\00",
39-
// section "__aix_copyright"
40-
// @llvm.used = appending global [1 x ptr] [ptr @__aix_copyright_str]
38+
// @__loadtime_copyright_str = internal constant [N x i8] c"Copyright\00",
39+
// section "__copyright_comment"
40+
// @llvm.used = appending global [1 x ptr] [ptr @__loadtime_copyright_str]
4141
//
4242
// define i32 @func() !implicit.ref !5 { ... }
43-
// !5 = !{ptr @__aix_copyright_str}
43+
// !5 = !{ptr @__loadtime_copyright_str}
4444
//
45-
// The copyright string is placed in the "__aix_copyright" section (mapped to
45+
// The copyright string is placed in the "__copyright_comment" section (mapped to
4646
// an XCOFF csect with [RO] storage class), making it easily identifiable in
4747
// object files and executables. The R_REF relocation prevents the linker
4848
// from discarding this section during garbage collection. Copyright string (if
@@ -90,9 +90,9 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
9090

9191
LLVMContext &Ctx = M.getContext();
9292

93-
// Single-metadata: !aix.copyright.comment = !{!0}
93+
// Single-metadata: !loadtime.copyright.comment = !{!0}
9494
// Each operand node is expected to have one MDString operand.
95-
NamedMDNode *MD = M.getNamedMetadata("aix.copyright.comment");
95+
NamedMDNode *MD = M.getNamedMetadata("loadtime.copyright.comment");
9696
if (!MD || MD->getNumOperands() == 0)
9797
return PreservedAnalyses::all();
9898

@@ -117,23 +117,23 @@ PreservedAnalyses CopyrightMetadataPass::run(Module &M,
117117
auto *StrGV = new GlobalVariable(M, StrInit->getType(),
118118
/*isConstant=*/true,
119119
GlobalValue::InternalLinkage, StrInit,
120-
/*Name=*/"__aix_copyright_str");
120+
/*Name=*/"__loadtime_copyright_str");
121121
// Set unnamed_addr to allow the linker to merge identical strings
122122
StrGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
123123
StrGV->setAlignment(Align(1));
124-
// Place in the "__aix_copyright" section.
124+
// Place in the "__copyright_comment" section.
125125
// Backend maps this to an appropriate XCOFF csect (typically [RO])
126126
// The section will appear in assembly as:
127-
// .csect __aix_copyright[RO],2
128-
StrGV->setSection("__aix_copyright");
127+
// .csect __copyright_comment[RO],2
128+
StrGV->setSection("__copyright_comment");
129129

130130
// 2. Add the string to llvm.used to prevent LLVM optimization/LTO passes from
131131
// removing it.
132132
appendToUsed(M, {StrGV});
133133

134134
// 3. Attach !implicit ref to every defined function
135135
// Create a metadata node pointing to the copyright string:
136-
// !N = !{ptr @__aix_copyright_str}
136+
// !N = !{ptr @__loadtime_copyright_str}
137137
Metadata *Ops[] = {ConstantAsMetadata::get(StrGV)};
138138
MDNode *ImplicitRefMD = MDNode::get(Ctx, Ops);
139139

llvm/test/CodeGen/PowerPC/pragma-comment-copyright-aix.ll

Lines changed: 0 additions & 61 deletions
This file was deleted.

llvm/test/Transforms/CopyrightMetadata/copyright-metadata.ll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
; RUN: opt --O2 -S %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-ON
77
; RUN: opt --O3 -S %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-ON
88

9-
; Verify that CopyrightMetadataPass lowers !aix.copyright.comment
9+
; Verify that CopyrightMetadataPass lowers !loadtime.copyright.comment
1010
; into concrete, translation-unit–local globals.
1111
;
1212
; For each module (translation unit), the pass performs the following:
1313
;
1414
; 1. Creates a null-terminated, internal constant string global
15-
; (`__aix_copyright_str`) containing the copyright text.
15+
; (`__loadtime_copyright_str`) containing the copyright text in
16+
; `__copyright_comment` section.
1617
;
1718
; 2. Marks the string in `llvm.used` so it cannot be dropped by
1819
; optimization or LTO.
@@ -37,14 +38,14 @@ entry:
3738
!llvm.module.flags = !{!0}
3839
!0 = !{i32 1, !"wchar_size", i32 2}
3940

40-
!aix.copyright.comment = !{!1}
41+
!loadtime.copyright.comment = !{!1}
4142
!1 = !{!"@(#) Copyright IBM 2025"}
4243

4344

4445
; ---- Globals--------------------------------------------
45-
; CHECK: @__aix_copyright_str = internal unnamed_addr constant [24 x i8] c"@(#) Copyright IBM 2025\00", section "__aix_copyright", align 1
46+
; CHECK: @__loadtime_copyright_str = internal unnamed_addr constant [24 x i8] c"@(#) Copyright IBM 2025\00", section "__copyright_comment", align 1
4647
; Preservation in llvm.used sets
47-
; CHECK-NEXT: @llvm.used = appending global [1 x ptr] [ptr @__aix_copyright_str], section "llvm.metadata"
48+
; CHECK-NEXT: @llvm.used = appending global [1 x ptr] [ptr @__loadtime_copyright_str], section "llvm.metadata"
4849
; CHECK-NOT: ![[copyright:[0-9]+]] = !{!"@(#) Copyright IBM 2025"}
4950

5051
; Function has an implicit ref MD pointing at the string:
@@ -54,5 +55,5 @@ entry:
5455
; CHECK-ON: define noundef i32 @main() local_unnamed_addr #0 !implicit.ref ![[MD]]
5556

5657
; Verify metadata content
57-
; CHECK-O0: ![[MD]] = !{ptr @__aix_copyright_str}
58-
; CHECK-ON: ![[MD]] = !{ptr @__aix_copyright_str}
58+
; CHECK-O0: ![[MD]] = !{ptr @__loadtime_copyright_str}
59+
; CHECK-ON: ![[MD]] = !{ptr @__loadtime_copyright_str}

0 commit comments

Comments
 (0)