This is an archive of the discontinued LLVM Phabricator instance.

[libLTO][AIX] Respect `-f[no]-integrated-as` on AIX
ClosedPublic

Authored by qiongsiwu1 on Jun 14 2023, 7:53 AM.

Details

Summary

libLTO currently ignores the -f[no-]integrated-as flags. This patch teaches libLTO to respect them on AIX.

The implementation consists of two parts:

  1. Migrate llc's -no-integrated-as option to a codegen option so that the option is available to libLTO/lld/gold.
  2. Teach clang to pass -no-integrated-as accordingly to libLTO depending on the -f[no-]integrated-as flags.

On platforms other than AIX, the -f[no-]integrated-as flags are ignored.

Diff Detail

Event Timeline

qiongsiwu1 created this revision.Jun 14 2023, 7:53 AM
qiongsiwu1 requested review of this revision.Jun 14 2023, 7:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 7:53 AM
qiongsiwu1 edited the summary of this revision. (Show Details)Jun 14 2023, 8:03 AM

lld does not use LTOCodeGenerator.

I am also not sure how disable integrated assembler suppose to work? Do you have an end to end design for how to invoke assembler during linking? For current libLTO API, this is probably going to produce assembler file buffer to linker, which is not usable by linker.

qiongsiwu1 retitled this revision from [libLTO][AIX] Respect `-f[no]-integrated-as` to [libLTO][AIX] Respect `-f[no]-integrated-as` on AIX.Jun 14 2023, 9:50 AM

lld does not use LTOCodeGenerator.

I am also not sure how disable integrated assembler suppose to work? Do you have an end to end design for how to invoke assembler during linking? For current libLTO API, this is probably going to produce assembler file buffer to linker, which is not usable by linker.

Thanks for chiming in @steven_wu! This patch is enabling -f[no-]-integrated-as on AIX only. There is already code to invoke the assembler during linking https://github.com/llvm/llvm-project/blob/807adcf4b9cca5586fd1fb669811768528d9dd1d/llvm/lib/LTO/LTOCodeGenerator.cpp#L343. clang will emit a warning on other platforms. The patch title is revised to reflect that this feature is AIX only at the moment.

steven_wu accepted this revision.Jun 14 2023, 3:44 PM

I see. Sounds fine to me.

This revision is now accepted and ready to land.Jun 14 2023, 3:44 PM

@qiongsiwu1 Hi, thanks for the patch. Almost good to me, except one small nit.

clang/lib/Driver/ToolChains/CommonArgs.cpp
698

Seems other options leverage the default value in the back end, for example the default value for DisableIntegratedAS in backend is false, so when the front end requires integrated-as, maybe we can save the option here?

qiongsiwu1 added inline comments.Jun 15 2023, 7:16 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

Ah thanks for the comment!

maybe we can save the option here?

Could you help me understand what we mean by "the option"? Do we mean saving (or using?) the value of -f[no]-integrated-as explicitly here somehow instead of relying on ToolChain.useIntegratedAs()? How do we intend to use the saved option value? My understanding is that DisableIntegratedAS takes its value from the option -no-integrated-as if -no-integrated-as is specified. As pointed out eariler, DisableIntegratedAS is false by default. This code explicitly sets -no-integrated-as to 0 or 1, so for the LTO use case, we overwrite the back end default since the option is always present.

shchenz added inline comments.Jun 15 2023, 7:24 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

For example, if front-end requires to use integrated-assembler which is same with back-end's default value, we don't need to pass -no-integrated-as=0? We only pass the option -no-integrated-as=1 when if (IsOSAIX && !UseIntegratedAs).

qiongsiwu1 added inline comments.Jun 15 2023, 7:45 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

Ah I see! Thanks for the clarification!

@w2yehia and I discussed this and we preferred setting the option explicitly regardless of the backend's default. The reason was that we did not want to leave a hanging case where the option was not passed to the backend, a case in which we would rely on a non-local option(DisableIntegratedAS)'s default value to control the backend. In other words, always passing in the option allowed us to reason about this code locally within this file. @w2yehia feel free to chime in if I am not describing our discussion correctly.

Could you help me understand the benefit of not passing in the option for the default case?

shchenz added inline comments.Jun 15 2023, 8:07 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

Could you help me understand the benefit of not passing in the option for the default case?

Too many options pass from front-end to back-end is a reason. And another reason is: I met one case that there is a back-end option has no default value, so each front-end, like clang and FORTRAN will have to explicitly pass the same option. I was asked to set a default in the back-end, so no need to explicitly set the options in each front-end.

If what I read is right, some bool type options like EmulatedTLS, EnableStackSizeSection are only passed when their values are not the same with the back-end's default.

I am ok to keep it as now if you guys already have an agreement. This is just minor I think.

Seems passing -Wl,-bplugin_opt:-no-integrated-as=1 from clang driver does not work as expected. Is this expected or by design(We can only use -fno-integrated-as but no -Wl,-bplugin_opt:-no-integrated-as=1)?

clang -flto -Wl,-bplugin_opt:-no-integrated-as=1 *.c -v
  -bplugin_opt:-no-integrated-as=1 -bplugin:../lib/libLTO.so -bplugin_opt:-mcpu=pwr7 -bplugin_opt:-no-integrated-as=0 ;;; Note the second -no-integrated-as=0 pass from clang front-end.
MaskRay added inline comments.Jun 16 2023, 12:01 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

If you are going to add -no-integrated-as=0, I suggest that you rename the option to -integrated-as and use -integrated-as=1 instead.

MaskRay added inline comments.Jun 16 2023, 12:05 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
701

The patch is titled AIX but this would change the Linux behavior. In my experience this diagnostic is not useful.

Revising the option processing logic to not pass in -no-integrated-as=0. Thanks @shchenz and @MaskRay for the feedback!

qiongsiwu1 marked 3 inline comments as done.Jun 16 2023, 5:54 AM
qiongsiwu1 added inline comments.
clang/lib/Driver/ToolChains/CommonArgs.cpp
698

-no-integrated-as=0 is no longer added. The backend default will take care of the case.

qiongsiwu1 marked an inline comment as done.Jun 16 2023, 6:02 AM

Seems passing -Wl,-bplugin_opt:-no-integrated-as=1 from clang driver does not work as expected. Is this expected or by design(We can only use -fno-integrated-as but no -Wl,-bplugin_opt:-no-integrated-as=1)?

clang -flto -Wl,-bplugin_opt:-no-integrated-as=1 *.c -v
  -bplugin_opt:-no-integrated-as=1 -bplugin:../lib/libLTO.so -bplugin_opt:-mcpu=pwr7 -bplugin_opt:-no-integrated-as=0 ;;; Note the second -no-integrated-as=0 pass from clang front-end.

Thanks for catching this! The latest patch fixed the problem. As you rightly pointed out, always passing in -no-integrated-as=0 by default interferes with uses such as above.

qiongsiwu1 added inline comments.Jun 16 2023, 6:18 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
701

Yes I agree the title is a bit misleading. The warning is added because the behaviour of -fno-integrated-as changes with and without -flto. Without -flto, clang does create the job to invoke system assembler, and with -flto, currently clang silently ignores the -fno-integrated-as option (without this patch). I felt that was unexpected, hence I added the warning. I am happy to remove the warning if the community thinks that silently ignoring the option is acceptable.

The limitation of this diagnostic is that it goes through clang and there is nothing in the backend guarding against using -no-integrated-as directly through -plugin-opt on Linux, which will be ignored. @MaskRay is this something we should worry about? If not, I will not try looking for a place to add the warning in the backend.

Thanks!

MaskRay added inline comments.Jun 16 2023, 8:15 AM
clang/lib/Driver/ToolChains/CommonArgs.cpp
701

I do not worry about LTO ignoring -fno-integrated-as on Linux/*BSD targets. The use cases are very limited and the primary user (Linux kernel community) knows this.

Actually the link action ignores compilation-affected options anyway (all CompileOnly_option).

qiongsiwu1 edited the summary of this revision. (Show Details)

Address code review.

qiongsiwu1 marked an inline comment as done.Jun 16 2023, 11:11 AM
qiongsiwu1 added inline comments.
clang/lib/Driver/ToolChains/CommonArgs.cpp
701

Thanks! The diagnostic is removed.

qiongsiwu1 marked 3 inline comments as done.Jun 16 2023, 11:12 AM

Clean up code in addLTOOptions.

@MaskRay @w2yehia @shchenz could you let me know if there are further comments? I will land this patch if there are none. Thanks!

MaskRay added inline comments.Jul 11 2023, 11:25 AM
clang/test/Driver/lto-aix.c
76

This ^//$ line is not useful. Some tests use this style, but it's not a good style.

If we remove this line, we can actually use Vim { } navigate among tests.

79

2-space indentation

llvm/test/tools/llvm-lto/aix-sys-as.ll
12 ↗(On Diff #532256)

delete trailing blank line

We have TargetOptions::DisableIntegratedAS (your llc.cpp change). Do you know why it is not feasible for the places you want to check?

We have TargetOptions::DisableIntegratedAS (your llc.cpp change). Do you know why it is not feasible for the places you want to check?

I am checking/using TargetOptions::DisableIntegratedAS in LTOCodeGenerator::useAIXSystemAssembler() to determine if the system assembler should be used. NoIntegratedAssembler is moved from an llc only option to a codegen option so LTO can use it as well.

MaskRay accepted this revision.Jul 11 2023, 12:56 PM

Some nits about testing, otherwise LG

We have TargetOptions::DisableIntegratedAS (your llc.cpp change). Do you know why it is not feasible for the places you want to check?

I am checking/using TargetOptions::DisableIntegratedAS in LTOCodeGenerator::useAIXSystemAssembler() to determine if the system assembler should be used. NoIntegratedAssembler is moved from an llc only option to a codegen option so LTO can use it as well.

Sorry, my question was not clear. Anyway, I figured it out.
We need to pass lto::Config to lto::backend with the correct CGFileType information. TargetOptions::DisableIntegratedAS is already present, but we don't initialize it in llvm/lib/CodeGen/CommandFlags.cpp.
Thanks. This looks good to me.

llvm/test/tools/llvm-lto/aix-sys-as.ll
1 ↗(On Diff #532256)

I suggest that you merge this test into aix.ll.

You can use

RUN: %if system-aix %{ ... %}

to only run a RUN line when the lit feature system-aix is available.

llvm/test/tools/llvm-lto/aix.ll
4

It seems that with or without -o, the behavior may be different?

In some lit configurations (e.g., Google's internal lit runner), the current working directory if a test invocation is read-only. We need to ensure that the directory is writable.

You can do

RUN: rm -rf %t && mkdir %t && cd %t`
RUN: llvm-as < %s -o a.bc
RUN: llvm-lto a.bc

Address test case review comments. Thanks so much @MaskRay for the feedback!

qiongsiwu1 marked 3 inline comments as done.Jul 12 2023, 7:03 AM
qiongsiwu1 edited the summary of this revision. (Show Details)Jul 12 2023, 7:05 AM
This revision was landed with ongoing or failed builds.Jul 12 2023, 10:23 AM
This revision was automatically updated to reflect the committed changes.