Skip to content

Commit

Permalink
[Clang][AIX][p] Manually Claim -p in front end
Browse files Browse the repository at this point in the history
The current implementation of `-p` does not claim the argument once it
is passed. Since it pushes `-pg` directly, it is only ever referred to
again when linking. As a result, when compiling with `-S`, the compiler
warns that `-p` goes unused even though that is not the case.

With this patch, if both `-p` and `-pg` are passed, the argument that is
passed second will take precedence. `-p` will still throw an error on
unsupported platforms, regardless of precedence.

This revision includes a test case, which has been placed in
`clang/test/Driver/zos-profiling-error.c`. As a result,
`zos-profiling-error.c` has been renamed to `ibm-profiling.c`. This
revision also passes `clang/test/Driver/aix-ld.c`.

Differential Revision: https://reviews.llvm.org/D145021
  • Loading branch information
KappaMikey1337 committed Mar 15, 2023
1 parent 9dbce77 commit ac1d143
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
16 changes: 8 additions & 8 deletions clang/lib/Driver/ToolChains/AIX.cpp
Expand Up @@ -164,14 +164,14 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}

auto getCrt0Basename = [&Args, IsArch32Bit] {
// Enable gprofiling when "-pg" is specified.
if (Args.hasArg(options::OPT_pg))
return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
// Enable profiling when "-p" is specified.
else if (Args.hasArg(options::OPT_p))
if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) {
// Enable gprofiling when "-pg" is specified.
if (A->getOption().matches(options::OPT_pg))
return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
// Enable profiling when "-p" is specified.
return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
else
return IsArch32Bit ? "crt0.o" : "crt0_64.o";
}
return IsArch32Bit ? "crt0.o" : "crt0_64.o";
};

if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
Expand Down Expand Up @@ -271,7 +271,7 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,

CmdArgs.push_back("-lc");

if (Args.hasArg(options::OPT_p, options::OPT_pg)) {
if (Args.hasArgNoClaim(options::OPT_p, options::OPT_pg)) {
CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
"/lib/profiled"));
CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
Expand Down
18 changes: 12 additions & 6 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -6322,20 +6322,26 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< A->getAsString(Args) << TripleStr;
}
}
if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) {
if (TC.getTriple().isOSAIX()) {
CmdArgs.push_back("-pg");
} else if (!TC.getTriple().isOSOpenBSD()) {

if (Arg *A = Args.getLastArgNoClaim(options::OPT_pg)) {
if (TC.getTriple().isOSzOS()) {
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< A->getAsString(Args) << TripleStr;
}
}
if (Arg *A = Args.getLastArgNoClaim(options::OPT_pg)) {
if (TC.getTriple().isOSzOS()) {
if (Arg *A = Args.getLastArgNoClaim(options::OPT_p)) {
if (!(TC.getTriple().isOSAIX() || TC.getTriple().isOSOpenBSD())) {
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< A->getAsString(Args) << TripleStr;
}
}
if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) {
if (A->getOption().matches(options::OPT_p)) {
A->claim();
if (TC.getTriple().isOSAIX() && !Args.hasArgNoClaim(options::OPT_pg))
CmdArgs.push_back("-pg");
}
}

if (Args.getLastArg(options::OPT_fapple_kext) ||
(Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
Expand Down
27 changes: 27 additions & 0 deletions clang/test/Driver/ibm-profiling.c
@@ -0,0 +1,27 @@
// Check that -pg throws an error on z/OS.
// RUN: %clang -### 2>&1 --target=s390x-none-zos -S -pg %s | FileCheck -check-prefix=FAIL-PG-NAME %s
// FAIL-PG-NAME: error: unsupported option '-pg' for target 's390x-none-zos'

// Check that -p is still used when not linking on AIX.
// RUN: %clang -### 2>&1 --target=powerpc-ibm-aix7.1.0.0 -S -p -S %s \
// RUN: | FileCheck --check-prefix=CHECK %s
// CHECK-NOT: warning: argument unused during compilation: '-p'

// Check precedence: -pg is unused when passed first on AIX.
// RUN: %clang -### 2>&1 --target=powerpc-ibm-aix7.1.0.0 --sysroot %S/Inputs/aix_ppc_tree -pg -p %s \
// RUN: | FileCheck --check-prefix=CHECK2 %s
// CHECK2-NOT: warning: argument unused during compilation: '-p' [-Wunused-command-line-argument]
// CHECK2: "-isysroot" "[[SYSROOT:[^"]+]]"
// CHECK2: "[[SYSROOT]]/usr/lib{{/|\\\\}}mcrt0.o"
// CHECK2: "-L[[SYSROOT]]/lib/profiled"
// CHECK2: "-L[[SYSROOT]]/usr/lib/profiled"

// Check precedence: -p is unused when passed first on AIX.
// RUN: %clang -### 2>&1 --target=powerpc-ibm-aix7.1.0.0 --sysroot %S/Inputs/aix_ppc_tree -p -pg %s \
// RUN: | FileCheck --check-prefix=CHECK3 %s
// CHECK3: warning: argument unused during compilation: '-p' [-Wunused-command-line-argument]
// CHECK3: "-isysroot" "[[SYSROOT:[^"]+]]"
// CHECK3: "[[SYSROOT]]/usr/lib{{/|\\\\}}gcrt0.o"
// CHECK3: "-L[[SYSROOT]]/lib/profiled"
// CHECK3: "-L[[SYSROOT]]/usr/lib/profiled"

2 changes: 0 additions & 2 deletions clang/test/Driver/zos-profiling-error.c

This file was deleted.

0 comments on commit ac1d143

Please sign in to comment.