This is an archive of the discontinued LLVM Phabricator instance.

Driver: Refactor and support per target dirs in baremetal
ClosedPublic

Authored by manojgupta on Aug 4 2022, 5:59 PM.

Details

Summary

Refactor baremetal driver code to reduce the bespoke
additions and base class overrides.
This lets us use the per target runtimes like other clang
targets. E.g. clang -target armv7m-cros-none-eabi will now
be able to use the runtimes installed at
<resource_dir>/lib/armv7m-cros-none-eabi instead of the hardcoded
path <resource_dir>/lib/baremetal.
The older code paths should still continue to work as before if
<resource_dir>/lib/<tuple> does not exist.

Diff Detail

Event Timeline

manojgupta created this revision.Aug 4 2022, 5:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2022, 5:59 PM
manojgupta requested review of this revision.Aug 4 2022, 5:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2022, 5:59 PM
abidh added a comment.Aug 8 2022, 4:59 AM

I think this patch is moving the things in the right direction. But please wait for some other reviewers to chime in.

I think this patch is moving the things in the right direction. But please wait for some other reviewers to chime in.

It seems quite opposite to me.

clang/include/clang/Driver/ToolChain.h
384

Is this a correct sentence? (My English is poor.)

388

The ToolChain class is an interface class. It is strange to see such kind of methods here. IsBareMetal should at least be virtual and overridden in concrete implementation of baremetal toolchains. IsRISCVBareMetal should not be here at all.
What was wrong with the previous implementation that made you move the methods here?

abidh added a comment.Aug 8 2022, 5:57 AM

I think this patch is moving the things in the right direction. But please wait for some other reviewers to chime in.

It seems quite opposite to me.

To be clear, my comment was aimed at using target specific directory instead of using lib/baremetal.

manojgupta added inline comments.Aug 8 2022, 9:44 AM
clang/include/clang/Driver/ToolChain.h
384

I'll fix it. Somehow I forgot to check them.

388

There is a need to check for IsBareMetal and variants. They can't be made virtual since the checks need to happen before instantiating ToolChain class. I think moving them to Triple class (Triple.h) is a clearer option.

barannikov88 added inline comments.Aug 8 2022, 11:31 AM
clang/include/clang/Driver/ToolChain.h
388

I think moving them to Triple class (Triple.h) is a clearer option.

Is the triple is all that is necessary to decide whether the target is bare metal or not?
Sounds interesting, but one may argue that Triple should not know about toolchains (like it should not know about C data type bit widths, for example).
What if just add a few switch-cases to Driver::getToolChain as for every other toolchain? Retaining the former static method 'BareMetalToolChain::handlesTarget' is still better in my opinion.

Moved Baremetal triple related code to Triple.h.

Herald added a project: Restricted Project. · View Herald TranscriptAug 8 2022, 11:41 AM
barannikov88 added inline comments.Aug 8 2022, 11:43 AM
clang/include/clang/Driver/ToolChain.h
388

They can't be made virtual since the checks need to happen before instantiating ToolChain class.

This is kind of two different things. It can be made virtual. The name of the method IsBareMetal of the ToolChain class suggests that it is checking whether this concrete instance of the ToolChain is baremetal. This (virtual) method could be used in getCompilerRTPath, for example.
To instantiate BareMetalToolchain you need another function (like the former BareMetalToolChain::handlesTarget, or the suggested approach with Triple). For these instantiations IsBareMetal will return true, and false for all other instantiations which are not bare metal.

manojgupta added inline comments.Aug 8 2022, 11:45 AM
clang/include/clang/Driver/ToolChain.h
388

Is the triple is all that is necessary to decide whether the target is bare metal or not?

At least for baremetal, from what I see, triple seems to be the way how baremetal toolchain is decided in practice.

Examples:

  1. https://gitweb.gentoo.org/proj/crossdev.git/tree/crossdev#n331 ( # Bare metal targets *-newlib|*-elf|*-eabi|*-rtems*)
  2. https://elinux.org/images/1/15/Anatomy_of_Cross-Compilation_Toolchains.pdf ( arm-foo-none-eabi, bare-metal toolchain targeting the ARM architecture, from

vendor foo )

barannikov88 added inline comments.Aug 8 2022, 11:46 AM
llvm/include/llvm/ADT/Triple.h
916 ↗(On Diff #450894)

Any future change to these methods will force recompilation of the whole project. It is better to move the definitions into the corresponding cpp file.

Moved defs to Triple.cpp

barannikov88 accepted this revision.Aug 8 2022, 12:17 PM

LGTM with the style issues fixed, but I'd like someone else to take a look.

clang/lib/Driver/ToolChains/BareMetal.cpp
279–287

There are a few style issues.

  • return is usually within the braces
  • Variable names should start with capital letter
  • Looks like std::string could be avoided completely in favor of StringRef and Twine

Same applies to ConstructJob below, there are also redundant braces in for statement.

This revision is now accepted and ready to land.Aug 8 2022, 12:17 PM
barannikov88 added inline comments.Aug 8 2022, 12:51 PM
llvm/lib/Support/Triple.cpp
1936 ↗(On Diff #450898)

(nit) at least llvm:: qualification is redundant, Triple:: might also be.

Address style nits.

Address style nits.

Neat, thank you!

Address more style lints.

manojgupta added inline comments.Aug 8 2022, 1:00 PM
clang/lib/Driver/ToolChains/BareMetal.cpp
279–287

Addressed most.
std::string is needed initially to avoid dangling storage issue:

warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]

MaskRay accepted this revision.Aug 9 2022, 11:12 AM
MaskRay added inline comments.
llvm/lib/Support/Triple.cpp
1938 ↗(On Diff #450910)

The prevailing style doesn't add a blank line for every if statement.

1980 ↗(On Diff #450910)

Add a blank line before the comment.

Address maskray comments.

manojgupta updated this revision to Diff 451325.Aug 9 2022, 5:30 PM

Fix clang-format complains.

manojgupta updated this revision to Diff 451352.Aug 9 2022, 9:31 PM

Going back to older handlesTarget() style in Baremetal.
Apparently RISCV can be both Baremetal or RISCVToolchain for the
same tuple. I do not know of the nuances so trying not to disturb that for
now.

Still LGTM

clang/lib/Driver/ToolChains/BareMetal.h
2

Line overflowed