This is an archive of the discontinued LLVM Phabricator instance.

[flang] Update tco tool pipline and add translation to LLVM IR
ClosedPublic

Authored by clementval on Jan 20 2022, 6:04 AM.

Details

Summary

tco is a tool to test the FIR to LLVM IR pipeline of the Flang compiler.

This patch update tco pipelines and adds the translation to LLVM IR.

A simple test is added to make sure the tool is working with a simple
FIR program.
More tests will be upstream in follow up patch from the fir-dev branch.

This patch is part of the upstreaming effort from fir-dev branch.

Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
Co-authored-by: Jean Perier <jperier@nvidia.com>
Co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com>

Diff Detail

Event Timeline

clementval created this revision.Jan 20 2022, 6:04 AM
clementval requested review of this revision.Jan 20 2022, 6:04 AM

What's the difference between tco and fir-opt? Just that the former can emit LLVM IR instead of LLVM dialect? And that the no-argument invocation will run the lowering pipeline?
For running pipeline, why not involve the flang binary? (this is what clang is doing by the way: it can take LLVM IR as input as well and emit LLVM IR, run various optimization pipeline, assembly, object, and handles the usual options you need anyway through frontend options. Between what you'd want to expose through flang anyway and what you already have with fir-opt, it's not clear to me the fundamental role tco plays? (I think I remember that it just existed before fir-opt and before there was a flang driver).

flang/tools/tco/tco.cpp
108

Why deferring this? Can't you propagate the failure?

What's the difference between tco and fir-opt? Just that the former can emit LLVM IR instead of LLVM dialect? And that the no-argument invocation will run the lowering pipeline?
For running pipeline, why not involve the flang binary? (this is what clang is doing by the way: it can take LLVM IR as input as well and emit LLVM IR, run various optimization pipeline, assembly, object, and handles the usual options you need anyway through frontend options. Between what you'd want to expose through flang anyway and what you already have with fir-opt, it's not clear to me the fundamental role tco plays? (I think I remember that it just existed before fir-opt and before there was a flang driver).

fir-opt doesn't have any defined pipeline and is similar to mlir-opt. We use it to test FIR to FIR passes. tco is meant to test the second half of the pipeline from FIR to LLVMIR. It has the same defined pipeline that the final Flang binary will have. At the moment we do not have a proper flang driver upstream but it will come in the last steps of the upstreaming from fir-dev. The pipeline is currently shared in fir-dev so we are sure we test the same things (defined in CLOptions.inc). Once we have a proper flang driver that have similar capabilities than clang we might change the test to make use of it and maybe retire tco.

flang/tools/tco/tco.cpp
108

Sure. I spotted that after I pushed the patch and I'll change it.

schweitz accepted this revision.Jan 20 2022, 10:10 AM
This revision is now accepted and ready to land.Jan 20 2022, 10:10 AM
mehdi_amini accepted this revision.EditedJan 20 2022, 9:57 PM

fir-opt doesn't have any defined pipeline and is similar to mlir-opt.

Sure, but that's trivial to register pipelines and expose them on the command line. I think it is even desirable to do so (for example opt in LLVM exposes -O2 on the command line).

We use it to test FIR to FIR passes.

I think you're providing the definition for fir-opt here?

tco is meant to test the second half of the pipeline from FIR to LLVMIR. It has the same defined pipeline that the final Flang binary will have.

Right, but again I don't understand why you don't expose this by registering a pipeline in fir-opt?

Once we have a proper flang driver that have similar capabilities than clang we might change the test to make use of it and maybe retire tco.

LGTM, assuming this tool is temporary and will get removed. But I'd like to understand why the pipeline can't be all registered in fir-opt?

fir-opt doesn't have any defined pipeline and is similar to mlir-opt.

Sure, but that's trivial to register pipelines and expose them on the command line. I think it is even desirable to do so (for example opt in LLVM exposes -O2 on the command line).

We use it to test FIR to FIR passes.

I think you're providing the definition for fir-opt here?

Yes it's fir-opt.

Once we have a proper flang driver that have similar capabilities than clang we might change the test to make use of it and maybe retire tco.

LGTM, assuming this tool is temporary and will get removed. But I'd like to understand why the pipeline can't be all registered in fir-opt?

I guess we can merge the two tools at some point. tco was there before fir-opt so nobody took the time to merge the functionality of one into the other yet.

Address the FIXME present in the previous patch

clementval marked an inline comment as done.Jan 21 2022, 1:23 AM

Add target to avoid failure on windows

Hi @clementval , thanks for this patch! I saw the buildbot failure that led to this being reverted: https://lab.llvm.org/buildbot/#/builders/177/builds/3311:

./bin/tco --target=aarch64-unknown-linux-gnu /home/kircha02/llvm-project/flang/test/Fir/basic-program.fir
: CommandLine Error: Option 'aarch64-enable-ccmp' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted (core dumped)

I took a quick look and I think that "flang/tools/tco/CMakeLists.txt" needs tweaking, In particular, LLVM_LINK_COMPONENTS is used in add_llvm_executable (which is called by add_flang_tool). So, I think that this will cause some libraries to be included twice in the list of dependencies:

target_link_libraries(tco PRIVATE
  ${llvm_libs}
)

which in turn would lead to some command line options being registered twice.

Also, llvm-config --components suggests that these aren't really components (I might be reading it wrong though):

set(LLVM_LINK_COMPONENTS
  AllTargetsAsmParsers
  AllTargetsCodeGens
  AllTargetsDescs
  AllTargetsInfos
)

Why not use ${LLVM_TARGETS_TO_BUILD} instead? HTH!

clementval reopened this revision.Jan 24 2022, 4:00 AM
This revision is now accepted and ready to land.Jan 24 2022, 4:00 AM

Update how the targets are linked to tco

This looks similar to other tools like: bolt/tools/driver/CMakeLists.txt

Hi @clementval , thanks for this patch! I saw the buildbot failure that led to this being reverted: https://lab.llvm.org/buildbot/#/builders/177/builds/3311:

./bin/tco --target=aarch64-unknown-linux-gnu /home/kircha02/llvm-project/flang/test/Fir/basic-program.fir
: CommandLine Error: Option 'aarch64-enable-ccmp' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted (core dumped)

I took a quick look and I think that "flang/tools/tco/CMakeLists.txt" needs tweaking, In particular, LLVM_LINK_COMPONENTS is used in add_llvm_executable (which is called by add_flang_tool). So, I think that this will cause some libraries to be included twice in the list of dependencies:

target_link_libraries(tco PRIVATE
  ${llvm_libs}
)

which in turn would lead to some command line options being registered twice.

Also, llvm-config --components suggests that these aren't really components (I might be reading it wrong though):

set(LLVM_LINK_COMPONENTS
  AllTargetsAsmParsers
  AllTargetsCodeGens
  AllTargetsDescs
  AllTargetsInfos
)

Why not use ${LLVM_TARGETS_TO_BUILD} instead? HTH!

Hi @awarzynski!

I figured out the same thing I just updated the patch to reflect that. Other tools like the new bolt-driver or clang and llvm tools are using the same mechanism.

Thanks for looking at this as well.

awarzynski accepted this revision.Jan 24 2022, 4:17 AM
awarzynski added inline comments.
flang/tools/tco/tco.cpp
40

Copy & paste error?

Passes the dylib build and tests.

clementval marked an inline comment as done.Jan 24 2022, 5:13 AM
clementval added inline comments.
flang/tools/tco/tco.cpp
40

That's what we have in fir-dev but it does not make so much sense. I'll just remove it and update fir-dev.

This revision was landed with ongoing or failed builds.Jan 24 2022, 5:16 AM
This revision was automatically updated to reflect the committed changes.
clementval marked an inline comment as done.
schweitz added inline comments.Jan 24 2022, 12:33 PM
flang/tools/tco/tco.cpp
40

Just garbage copied over from elsewhere. Thanks for deleting it.

Why does this tool even need to initialize the targets? It is a bit surprising that this linking dependency is declared on the tool instead of on the actual library that needs it.

It is a bit surprising that this linking dependency is declared on the tool instead of on the actual library that needs it.

IIUC, these dependencies are not needed at all. tco is not meant for interfacing with the backend (i.e. machine code generation). I've submitted an update: https://reviews.llvm.org/D118112

Apologies for not pointing this out earlier. I was away for a bit and when I looked at this yesterday I focused on fixing the build failure.

Btw, I agree that merging fir-opt and tco into one tool would be a sensible direction.

LGTM, assuming this tool is temporary and will get removed. But I'd like to understand why the pipeline can't be all registered in fir-opt?

I guess we can merge the two tools at some point. tco was there before fir-opt so nobody took the time to merge the functionality of one into the other yet.

To be very clear in this revision because there seems to be some confusion in the chat:

  • You got a conditional LGTM here and you committed based on this condition.
  • You acknowledged that you're contributed a tool here that only exists because the functionalities haven't been merged yet to fir-opt, but that we can merge the tools in fir-opt (and so delete tco at this point) in the future.

If this isn't very clear, please revert ASAP.

LGTM, assuming this tool is temporary and will get removed. But I'd like to understand why the pipeline can't be all registered in fir-opt?

I guess we can merge the two tools at some point. tco was there before fir-opt so nobody took the time to merge the functionality of one into the other yet.

To be very clear in this revision because there seems to be some confusion in the chat:

  • You got a conditional LGTM here and you committed based on this condition.
  • You acknowledged that you're contributed a tool here that only exists because the functionalities haven't been merged yet to fir-opt, but that we can merge the tools in fir-opt (and so delete tco at this point) in the future.

If this isn't very clear, please revert ASAP.

LGTM, assuming this tool is temporary and will get removed. But I'd like to understand why the pipeline can't be all registered in fir-opt?

I guess we can merge the two tools at some point. tco was there before fir-opt so nobody took the time to merge the functionality of one into the other yet.

To be very clear in this revision because there seems to be some confusion in the chat:

  • You got a conditional LGTM here and you committed based on this condition.
  • You acknowledged that you're contributed a tool here that only exists because the functionalities haven't been merged yet to fir-opt, but that we can merge the tools in fir-opt (and so delete tco at this point) in the future.

If this isn't very clear, please revert ASAP.

As I mentioned in the chat, we do not have the equivalent of tco at this point so it cannot be removed now.

Also to make things clearer the tco tool is the equivalent of mlir-translate in MLIR and fir-opt is the equivalent of mlir-opt.

As I mentioned in the chat, we do not have the equivalent of tco at this point so it cannot be removed now.

Sure, that part was very clear above. But we also need to agree that:

  1. there is a redundancy that isn't justified other than history (you wrote: nobody took the time to merge the functionality of one into the other yet)
  2. this redundancy is not desirable and so tco is meant to disappear.

Also to make things clearer the tco tool is the equivalent of mlir-translate in MLIR and fir-opt is the equivalent of mlir-opt.

This is not clear at all actually, on the contrary: this even goes against the entire discussion in this revision which was about comparing tco and fir-opt.
This has nothing to do with mlir-translate which is purely about converting from an MLIR dialect to an external representation (basically exporting outside of MLIR), and nothing else. In particular mlir-translate does not run "MLIR to MLIR" transformations, and does not invoke passes.

As I mentioned in the chat, we do not have the equivalent of tco at this point so it cannot be removed now.

Sure, that part was very clear above. But we also need to agree that:

  1. there is a redundancy that isn't justified other than history (you wrote: nobody took the time to merge the functionality of one into the other yet)
  2. this redundancy is not desirable and so tco is meant to disappear.

Also to make things clearer the tco tool is the equivalent of mlir-translate in MLIR and fir-opt is the equivalent of mlir-opt.

This is not clear at all actually, on the contrary: this even goes against the entire discussion in this revision which was about comparing tco and fir-opt.
This has nothing to do with mlir-translate which is purely about converting from an MLIR dialect to an external representation (basically exporting outside of MLIR), and nothing else. In particular mlir-translate does not run "MLIR to MLIR" transformations, and does not invoke passes.

The part dealing with MLIR to MLIR transformations can be removed from tco and place in fir-opt but the translation to LLVM IR is very much what mlir-translate does. So I agree that a part of tco would be redundant when moved to fir-opt but not the entire tool.

Right, and at this point isn't it identical mlir-translate?

Right, and at this point isn't it identical mlir-translate?

Probably. I didn't check whether there is a difference in the translation phase but my guess would be that they are pretty similar.

@mehdi_amini, your approval was indeed conditional and we should've followed-up with a discussion on the future of tco. In the past, we only briefly discussed replacing it with flang-new. That was a while back, before flang-new could generate MLIR/LLVM IR/a.out. The situation is a bit different now and it does sound like with a bit of extra work we should be able to replace it with other tools. One major outstanding functionality in flang-new is the ability to consume MLIR. We've not really looked into it yet, but I can prioritize it.

Btw, I read @clementval's

we might change the test to make use of it and maybe retire tco

as:

it would make sense, but perhaps lets re-visit once upstreaming is complete?

Quite a few tests in fir-dev still use tco and retiring it now would require people shifting their focus quite a bit. It will be much easier once fir-dev is "read-only". Having said that, I think that Valentin has already initiated the transition when he introduced fir-opt. To support the transition, I can retrofit flang-new to all tests that use tco (where possible). Long term, I do support tco being retired unless we can identify a unique feature that we cannot replicate with one of the other tools.

Just my 2p.

@mehdi_amini, your approval was indeed conditional and we should've followed-up with a discussion on the future of tco. In the past, we only briefly discussed replacing it with flang-new. That was a while back, before flang-new could generate MLIR/LLVM IR/a.out. The situation is a bit different now and it does sound like with a bit of extra work we should be able to replace it with other tools. One major outstanding functionality in flang-new is the ability to consume MLIR. We've not really looked into it yet, but I can prioritize it.

To be clear: I don't think there is a strong need to change any priority (at least I am not aware of anything).

Btw, I read @clementval's

we might change the test to make use of it and maybe retire tco

as:

it would make sense, but perhaps lets re-visit once upstreaming is complete?

Just note that what you're quoting is not the most recent answer (which was I guess we can merge the two tools at some point. tco was there before fir-opt so nobody took the time to merge the functionality of one into the other yet.).

Quite a few tests in fir-dev still use tco and retiring it now would require people shifting their focus quite a bit. It will be much easier once fir-dev is "read-only".

Yes, which is why I was perfectly fine to add it now to help the upstreaming. But that required an agreement about the future plans, hence the conditional approval. There is no urgency to do anything, if it takes a year to get there it's perfectly fine as well, but there is a difference between "yes this is redundant, 'nobody took the time to merge the functionality' yet, and we'll address this post-upstreaming" and "we don't know what we may or may not want to do".

Long term, I do support tco being retired unless we can identify a unique feature that we cannot replicate with one of the other tools.

I've not been asking anything more than an agreement on this :)

I've not been asking anything more than an agreement on this :)

Great, I think that we are on the same page here! :)

This has been very constructive and I think that it has really helped to clarify the status of tco (which is the most contentious element of this patch). Thank you all!