Skip to content

Commit 696ca9f

Browse files
author
Tony Varghese
committed
Chnaged to platform agnostic names for llvm metadata and variables used
1 parent f47b0d4 commit 696ca9f

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
@@ -42,6 +42,7 @@
4242
#include "clang/Basic/Diagnostic.h"
4343
#include "clang/Basic/DiagnosticFrontend.h"
4444
#include "clang/Basic/Module.h"
45+
#include "clang/Basic/PragmaKinds.h"
4546
#include "clang/Basic/SourceManager.h"
4647
#include "clang/Basic/TargetInfo.h"
4748
#include "clang/Basic/Version.h"
@@ -1569,10 +1570,10 @@ void CodeGenModule::Release() {
15691570

15701571
EmitBackendOptionsMetadata(getCodeGenOpts());
15711572

1572-
// Emit copyright metadata for AIX
1573-
if (AIXCopyrightComment) {
1574-
auto *NMD = getModule().getOrInsertNamedMetadata("aix.copyright.comment");
1575-
NMD->addOperand(AIXCopyrightComment);
1573+
// Emit copyright metadata
1574+
if (CopyrightCommentInTU) {
1575+
auto *NMD = getModule().getOrInsertNamedMetadata("loadtime.copyright.comment");
1576+
NMD->addOperand(CopyrightCommentInTU);
15761577
}
15771578

15781579
// If there is device offloading code embed it in the host now.
@@ -3410,17 +3411,20 @@ void CodeGenModule::AddDependentLib(StringRef Lib) {
34103411

34113412
/// Process the #pragma comment(copyright, "copyright string ")
34123413
/// and create llvm metadata for the copyright
3413-
void CodeGenModule::ProcessPragmaCommentCopyright(StringRef Comment) {
3414+
void CodeGenModule::ProcessPragmaComment(PragmaMSCommentKind Kind,
3415+
StringRef Comment) {
34143416

3415-
if (!getTriple().isOSAIX())
3417+
if (!getTriple().isOSAIX() || Kind != PCK_Copyright)
34163418
return;
34173419

3420+
assert(
3421+
!CopyrightCommentInTU &&
3422+
"Only one copyright comment should be present in the Translation Unit");
34183423
// Create llvm metadata with the comment string
34193424
auto &C = getLLVMContext();
34203425
llvm::Metadata *Ops[] = {llvm::MDString::get(C, Comment.str())};
34213426
auto *Node = llvm::MDNode::get(C, Ops);
3422-
if(!AIXCopyrightComment)
3423-
AIXCopyrightComment = Node;
3427+
CopyrightCommentInTU = Node;
34243428
}
34253429

34263430
/// Add link options implied by the given module, including modules
@@ -7604,7 +7608,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
76047608
// Skip pragmas deserialized from modules/PCHs
76057609
if (PCD->isFromASTFile())
76067610
break;
7607-
ProcessPragmaCommentCopyright(PCD->getArg());
7611+
ProcessPragmaComment(PCD->getCommentKind(), PCD->getArg());
76087612
break;
76097613
case PCK_Compiler:
76107614
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 {
@@ -3205,14 +3205,26 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32053205
// Verify that this is one of the 5 explicitly listed options.
32063206
IdentifierInfo *II = Tok.getIdentifierInfo();
32073207
PragmaMSCommentKind Kind =
3208-
llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
3209-
.Case("linker", PCK_Linker)
3210-
.Case("lib", PCK_Lib)
3211-
.Case("compiler", PCK_Compiler)
3212-
.Case("exestr", PCK_ExeStr)
3213-
.Case("user", PCK_User)
3214-
.Case("copyright", PCK_Copyright)
3215-
.Default(PCK_Unknown);
3208+
llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
3209+
.Case("linker", PCK_Linker)
3210+
.Case("lib", PCK_Lib)
3211+
.Case("compiler", PCK_Compiler)
3212+
.Case("exestr", PCK_ExeStr)
3213+
.Case("user", PCK_User)
3214+
.Case("copyright", PCK_Copyright)
3215+
.Default(PCK_Unknown);
3216+
3217+
// Restrict copyright to AIX targets only
3218+
if (!PP.getTargetInfo().getTriple().isOSAIX()) {
3219+
switch (Kind) {
3220+
case PCK_Copyright:
3221+
Kind = PCK_Unknown;
3222+
break;
3223+
default:
3224+
break;
3225+
}
3226+
}
3227+
32163228
if (Kind == PCK_Unknown) {
32173229
PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
32183230
return;
@@ -3225,13 +3237,13 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32253237
}
32263238

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

32373249
// Read the optional string if present.
@@ -3260,11 +3272,9 @@ void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
32603272
return;
32613273
}
32623274

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

32693279
// If the pragma is lexically sound, notify any interested PPCallbacks.
32703280
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)