Skip to content

[llvm][TableGen] Count implicit defs as well as explicit ones in the GlobalISel TableGen emitter #112673

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

Merged
merged 3 commits into from
Oct 18, 2024

Conversation

JL2210
Copy link
Contributor

@JL2210 JL2210 commented Oct 17, 2024

NumDefs only counts the number of registers in (outs), not any implicit defs specified with Defs = [...]

This causes patterns with physical register defs to fail to import here instead of later where implicit defs are rendered.

Add on ImplicitDefs.size() to count both and create DstExpDefs to count only explicit defs, used later on.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-tablegen

@llvm/pr-subscribers-llvm-globalisel

Author: None (JL2210)

Changes

NumDefs only counts the number of registers in (outs), not any implicit defs specified with Defs = [...]

Might want to use P.getDstRegs.size() instead of DstI.ImplicitDefs.size(), although then I think this would need to be an assert?


Full diff: https://github.com/llvm/llvm-project/pull/112673.diff

2 Files Affected:

  • (added) llvm/test/TableGen/GlobalISelEmitter-implicit-defs.td (+13)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+5-2)
diff --git a/llvm/test/TableGen/GlobalISelEmitter-implicit-defs.td b/llvm/test/TableGen/GlobalISelEmitter-implicit-defs.td
new file mode 100644
index 00000000000000..d1fb61db8c92b4
--- /dev/null
+++ b/llvm/test/TableGen/GlobalISelEmitter-implicit-defs.td
@@ -0,0 +1,13 @@
+// RUN: llvm-tblgen -gen-global-isel -warn-on-skipped-patterns -I %p/../../include -I %p/Common %s -o /dev/null 2>&1 < %s | FileCheck %s
+
+include "llvm/Target/Target.td"
+include "GlobalISelEmitterCommon.td"
+
+// CHECK-NOT: Skipped pattern: Src pattern result has 1 def(s) without the HasNoUse predicate set to true but Dst MI has no def
+// CHECK: Skipped pattern: Pattern defines a physical register
+let Uses = [B0], Defs = [B0] in
+def tst1 : I<(outs), (ins), [(set B0, (add B0, 1))]>;
+
+// CHECK: Skipped pattern: Src pattern result has 1 def(s) without the HasNoUse predicate set to true but Dst MI has no def
+let Uses = [B0] in
+def tst2 : I<(outs), (ins), [(set B0, (add B0, 1))]>;
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index c53f705a38db8f..29c64ba95ff856 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -2023,7 +2023,10 @@ Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
   auto &DstI = Target.getInstruction(DstOp);
   StringRef DstIName = DstI.TheDef->getName();
 
-  unsigned DstNumDefs = DstI.Operands.NumDefs,
+  // Count both implicit and explicit defs in the dst instruction.
+  // This avoids errors importing patterns that have inherent implicit defs.
+  unsigned DstExpDefs = DstI.Operands.NumDefs,
+           DstNumDefs = DstI.ImplicitDefs.size() + DstExpDefs,
            SrcNumDefs = Src.getExtTypes().size();
   if (DstNumDefs < SrcNumDefs) {
     if (DstNumDefs != 0)
@@ -2045,7 +2048,7 @@ Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
   // The root of the match also has constraints on the register bank so that it
   // matches the result instruction.
   unsigned OpIdx = 0;
-  unsigned N = std::min(DstNumDefs, SrcNumDefs);
+  unsigned N = std::min(DstExpDefs, SrcNumDefs);
   for (unsigned I = 0; I < N; ++I) {
     const TypeSetByHwMode &VTy = Src.getExtType(I);
 

@DavidSpickett DavidSpickett changed the title Count implicit defs as well as explicit ones in the GlobalISel TableGen emitter. [llvm][TableGen] Count implicit defs as well as explicit ones in the GlobalISel TableGen emitter Oct 18, 2024
@DavidSpickett
Copy link
Collaborator

@JL2210 I can merge this for you, please update the PR's description to the content you wish the commit message to have.

Generally this means describing what the changes do and why, and removing any other content you added as questions to reviewers.

…en emitter.

NumDefs only counts the number of registers in (outs), not any
implicit defs specified with Defs = [...]
@JL2210 JL2210 force-pushed the count-implicit-defs branch from 0c3c066 to 28094f7 Compare October 18, 2024 09:42
@JL2210
Copy link
Contributor Author

JL2210 commented Oct 18, 2024

@JL2210 I can merge this for you, please update the PR's description to the content you wish the commit message to have.

Generally this means describing what the changes do and why, and removing any other content you added as questions to reviewers.

I think I'm happy with it now.

@DavidSpickett DavidSpickett merged commit 8f6d491 into llvm:main Oct 18, 2024
5 of 6 checks passed
Copy link

@JL2210 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@JL2210 JL2210 deleted the count-implicit-defs branch October 19, 2024 01:06
@s-barannikov
Copy link
Contributor

@JL2210 After this commit I get a hard error when trying to import this pattern:

def : Pat<(nm_ineg i32:$val), (NEGrf i32:$val)>;
NMInstrPatterns.td:259:1: error: Operand DstI[rs2] was not declared in matcher
def : Pat<(nm_ineg i32:$val), (NEGrf i32:$val)>;

Here, nm_ineg is a two-result SD node and NEGrf is an instruction with one explicit def and one implicit def. rs2 is the name of the only operand.

Previously, this pattern was skipped with a warning:

Skipped pattern: Src pattern result has more defs than dst MI (2 def(s) vs 1 def(s))
def : Pat<(nm_ineg i32:$val), (NEGrf i32:$val)>;

@JL2210
Copy link
Contributor Author

JL2210 commented Oct 19, 2024

@s-barannikov Sorry about that. Do you think you might be able to come up with a minimal example/testcase that fails this, so I can debug locally?

I'm in the process of writing a followup to this that I think might fix the issue.

@s-barannikov
Copy link
Contributor

I'll prepare a testcase

@s-barannikov
Copy link
Contributor

s-barannikov commented Oct 20, 2024

Here is a reduced td file
test.td.txt

$ mv test.td.txt test.td
$ <...>/bin/llvm-tblgen -gen-global-isel test.td -I <...>/llvm/include

@s-barannikov
Copy link
Contributor

To be clear, the issue is not urgent, I can just comment out GINodeEquiv line since the pattern wasn't imported anyway.

@JL2210
Copy link
Contributor Author

JL2210 commented Oct 20, 2024

@s-barannikov Thanks! I'll work on it.

@JL2210
Copy link
Contributor Author

JL2210 commented Oct 20, 2024

Luckily, this is what I was expecting. Printed a backtrace when this happened:

#11 0x0000600460d896b3 (anonymous namespace)::GlobalISelEmitter::importExplicitDefRenderers(std::_List_iterator<std::unique_ptr<llvm::gi::MatchAction, std::default_delete<llvm::gi::MatchAction>>>, llvm::gi::RuleMatcher&, llvm::gi::BuildMIAction&, llvm::TreePatternNode const&, llvm::TreePatternNode const&, unsigned int) /home/james/git/llvm-project/llvm/utils/TableGen/GlobalISelEmitter.cpp:1547:32
#12 0x0000600460d85087 (anonymous namespace)::GlobalISelEmitter::createAndImportInstructionRenderer(llvm::gi::RuleMatcher&, llvm::gi::InstructionMatcher&, llvm::TreePatternNode const&, llvm::TreePatternNode const&) /home/james/git/llvm-project/llvm/utils/TableGen/GlobalISelEmitter.cpp:1372:16

so I'm on the right track, at least

JL2210 added a commit to JL2210/llvm-project that referenced this pull request Oct 21, 2024
Most of the heavy lifting was already done in
importExplicitDefRenderers, so I just added an operand for the
physical register defs.

The logic introduced in llvm#112673 was indeed subtly broken; it was
only supposed to count the physical register defs present in the
pattern rather than all physical register defs.

Remove importImplicitDefRenderers since it was unimplemented
anyway, and I don't see a way that it could be implemented
satisfactorily. Also, the name was rather misleading; it should've
been called something like importPhysRegDefRenderers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants