-
Notifications
You must be signed in to change notification settings - Fork 18.5k
[llc] set canonical triple in module #203725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ; REQUIRES: aarch64-registered-target | ||
| ; REQUIRES: default_triple | ||
|
|
||
| ;; Verify that llc correctly sets the module triple when one is not present. | ||
|
|
||
| ; RUN: llc -march=aarch64 -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MARCH | ||
| ; RUN: llc -mtriple=aarch64-unknown-linux-gnu -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MTRIPLE | ||
| ; RUN: llc -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=DEFAULT | ||
|
|
||
| ; MARCH: target triple = "aarch64-{{.*}}-{{.*}}" | ||
| ; MTRIPLE: target triple = "aarch64-unknown-linux-gnu" | ||
| ; DEFAULT: target triple = "{{.*}}-{{.*}}-{{.*}}" | ||
|
|
||
| define void @f() { ret void } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ; REQUIRES: aarch64-registered-target | ||
| ; REQUIRES: x86-registered-target | ||
|
|
||
| ;; Verify that llc correctly sets the module triple when one is present. | ||
|
|
||
| ; RUN: llc -march=aarch64 -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MARCH | ||
| ; RUN: llc -mtriple=aarch64-unknown-unknown -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MTRIPLE | ||
| ; RUN: llc -stop-after=finalize-isel -o - %s | FileCheck %s --check-prefix=MODULE | ||
|
|
||
| ; MARCH: target triple = "aarch64-unknown-linux-gnu" | ||
| ; MTRIPLE: target triple = "aarch64-unknown-unknown" | ||
| ; MODULE: target triple = "x86_64-unknown-linux-gnu" | ||
|
|
||
| target triple = "x86_64-unknown-linux-gnu" | ||
|
|
||
| define void @f() { ret void } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -675,8 +675,8 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList, | |||||||||||||||||||||||||||
| Err.print(argv[0], WithColor::error(errs(), argv[0])); | ||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| if (!TargetTriple.empty()) | ||||||||||||||||||||||||||||
| M->setTargetTriple(Triple(Triple::normalize(TargetTriple))); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| M->setTargetTriple(TheTriple); | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
AFAICT, I think the old code was doing the right thing. Can you elaborate on specific examples of triple divergence you want to fix?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
llvm-project/llvm/tools/llc/llc.cpp Lines 651 to 652 in 6f8ec43
I'm specifically trying to fix the case where LLVM_DEFAULT_TARGET_TRIPLE is set to nvptx and a module doesn't have a triple. Currently we have lots of issues on this path because passes like ExpandVariadics and TargetLibraryInfo consult the (previously unset) module triple to decide what to do. This is why the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In that case, should't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The guard would need to be a more complex. We also want to update when
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I guess regardless of what the module's triple says, we're about to compile it with the current compiler triple. So that addresses the "should we set the triple unconditionally" part. However, I'm not sure what's the right value to set it to. I still think that command-line should have precedence. Based on module-triple.ll test, it already seems to work that way. It may be worth adding a comment that this triple may be further overridden somewhere else (or, maybe, it gets derived from -mtriple before?).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
llvm-project/llvm/tools/llc/llc.cpp Lines 634 to 644 in 6f8ec43
Here is where |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| std::optional<CodeModel::Model> CM_IR = M->getCodeModel(); | ||||||||||||||||||||||||||||
| if (!CM && CM_IR) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this behavior makes any sense. Why should the command line override the triple in the file?
The command line triple is only really useful in tests where you want to have no triple in the file, and then test with multiple triples. I think it would be less surprising if this errored if the command line triple doesn't match the module triple.
Also, whatever the behavior is, it should match opt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
-mtriplecommand line flag has long overridden the triple set in the file in bothoptandllc. I don't want to open the can of worms that changing that would be nor do I think it is directly related to this PR.This change is just about making sure that irrespective of what precedence rules we use to choose the canonical triple, that triple is consistent across both the module and the TM. This is important to prevent crashes and mis-compilations. I'm specifically trying to fix the case where
LLVM_DEFAULT_TARGET_TRIPLEis set tonvptxand a module doesn't have a triple. Currently we have lots of issues on this path because passes likeExpandVariadicsandTargetLibraryInfoconsult the (previously unset) module triple to decide what to do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My general view is that more specific input should be able to override less specific one.
I.e. compiler default < something specified in the source file < something specified on command line.
The override may not always be valid, but that's a user problem. Nothing stops me trying to compile fortran source as C++ if I specify
-x c++. It does not make a lot of sense, but it's not the reason to ignore explicit user input.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not user facing tools. They exist solely for lit tests and developer experiments. Flexibility is a big minus, we should prefer to error on mismatches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagnostics are fine when they make sense. I'm not sure what/how we'd diagnose here.
I'm of the opposite view. IMO, it's the user-facing apps where we need strict validity checks, and it's development is where we can/should allow maximum flexibility and less guardrails. Presumably developers should know what they are doing with those tools, while the users operate on documentation-based contract (ish).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have never wanted to do this. IR is target specific from construction, and running another target's IR through llc is just looking for less-than-useful bug reports. This is only useful as a stochastic fuzzing technique for unexpected inputs. The realistic use case for triple-from-command-line is when you want to compile multiple targets in the same lit test, like x86 tests 32/64 bit case. In which case the file shouldn't contain a triple and llc should set it.
Because of the element of surprise. I do not expect an arbitrary IR sample to work across all targets, especially if that's IR from any real program.
Doing that would be an improvement over the status quo, I don't think this is. The module's triple should be set, but not if it conflicts with the command line's. I also don't think this would be a particularly pervasive change, did you try?
I didn't follow the specific here, but CodeGen/Generic tests simply shouldn't exist. You cannot have a stable test that works on an arbitrary backends, and this is a frequent source of build bot breakage. These should be sharded out into individual targets (though the common case here just needs to pick one as a sample).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's longstanding disagreement on this point. It is true that LLVM IR is not in general portable, it is a compiler IR (@sunfishcode ), but there is a portable core to it, and we have rejected proposals that would make LLVM IR lower-level (i.e. encoding which parameters are passed in memory in the frontend to memory optimizations) if it would make that core IR less portable.
I think shouldn't exist goes too far. I think we could solve all the problems you mentioned with some lit templating logic to stamp out llc RUN lines for all registered targets with standard triples, with generated CHECK-${ARCH} lines.
I think that would actually be a substantial improvement over the current situation of needing to copy and maintain tests for generic features (i128, musttail, byval) over to every architecture and to keep them in sync. Improvements to test coverage of a generic feature end up carrying over to every backend. It's a problem that llvm/tests is 1GiB of generated code that produces brittle, unreviewable diffs, and we should try to do something about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an improvement over the status quo even assuming that we want your preferred behavior. It seems like we're agreed that the triple used to select the TM and the triple in the module should agree and that there are cases, such as when no triple is specified in the module, where we would want to set the module triple to whatever
llcdecides it should be. All this change does is ensure this stamping occurs. The logic for triple precedence/erroring remains the status quo.I just gave it a try and there are hundreds of tests which would need to be updated. Most can probably be fixed by simply removing the triple in the module but some are more tricky such as this:
llvm-project/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.workitem.id.ll
Lines 1 to 7 in d982e04
I'm sure there's a way to make them all pass but I don't really have the time or expertise to address them all and I don't think this change actually needs to expand to cover that. This is a 1-line targeted fix primarily for cases where the default triple is used. I don't think it makes sense to expand the scope to re-litigate design decisions in
llc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @AlexMaclean on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That example would be solved by using isCompatibleWith instead of string equality