Skip to content

[LLVM] BasicTTIImpl allow unknown type during legality checking #89848

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 2 commits into from
May 3, 2024

Conversation

zyx-billy
Copy link
Contributor

@zyx-billy zyx-billy commented Apr 23, 2024

Make BasicTTIImplBase's isTypeLegal check handle unknown types. Current behavior is aborting.

Motivated by a use case in SimplifyCFG, where isTypeLegal is called on a struct type and dies, when it could be treated as illegal and skipped. In general it could make sense for unknown types to be allowed, and by default just considered not legal, but the behavior can of course be overriden. Please LMK if this is too lenient for other use cases (we can consider parameterizing it too).

@zyx-billy zyx-billy marked this pull request as ready for review April 24, 2024 17:59
@llvmbot
Copy link
Member

llvmbot commented Apr 24, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Billy Zhu (zyx-billy)

Changes

Make BasicTTIImplBase's isTypeLegal check handle unknown types. Current behavior is aborting.

Motivated by a use case in SimplifyCFG, where isTypeLegal is called on a struct type and dies, when it could be treated as illegal and skipped. In general it could make sense for unknown types to be allowed, and by default just considered not legal, but the behavior can of course be overriden. Please LMK if this is too lenient for other use cases (we can consider parameterizing it too).


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

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/BasicTTIImpl.h (+1-1)
  • (modified) llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll (+22)
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 06a19c75cf873a..ad7edcf7f9d6a7 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -426,7 +426,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
   bool useAA() const { return getST()->useAA(); }
 
   bool isTypeLegal(Type *Ty) {
-    EVT VT = getTLI()->getValueType(DL, Ty);
+    EVT VT = getTLI()->getValueType(DL, Ty, /*AllowUnknown=*/true);
     return getTLI()->isTypeLegal(VT);
   }
 
diff --git a/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll b/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
index 3873f0c0ae0bbd..9e3bb2368e7a4d 100644
--- a/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
+++ b/llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
@@ -2068,3 +2068,25 @@ cond.end:                                         ; preds = %entry, %cond.false
   %conv = sext i3 %cond to i8
   ret i8 %conv
 }
+
+; Don't create a table with an unknown type
+define { i8, i8 } @test_unknown_result_type(i8 %n) {
+; CHECK-LABEL: @test_unknown_result_type(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: switch
+entry:
+  switch i8 %n, label %sw.default [
+    i8 0, label %return
+    i8 1, label %return
+    i8 2, label %return
+  ]
+
+sw.default:                                       ; preds = %entry
+  %0 = insertvalue { i8, i8 } undef, i8 0, 0
+  %1 = insertvalue { i8, i8 } %0, i8 1, 1
+  br label %return
+
+return:                                           ; preds = %sw.default, %entry, %entry, %entry
+  %retval.0 = phi { i8, i8 } [ undef, %entry ], [ undef, %entry ], [ undef, %entry ], [ %1, %sw.default ]
+  ret { i8, i8 } %retval.0
+}

@zyx-billy zyx-billy requested a review from lukel97 April 26, 2024 00:33
@Mogball
Copy link
Contributor

Mogball commented Apr 30, 2024

LGTM but please get a review from someone else

@zyx-billy zyx-billy requested a review from khei4 May 1, 2024 16:39
@khei4 khei4 requested review from nikic and dtcxzyw May 2, 2024 05:45
@zyx-billy zyx-billy force-pushed the llvm/tti_check_type_fix branch from 0e766c4 to b9f0402 Compare May 2, 2024 18:38
@khei4 khei4 requested a review from dianqk May 2, 2024 22:55
@khei4
Copy link
Contributor

khei4 commented May 2, 2024

LGTM. But I think it's better to be reviewed by recently active middle-end committers.
@nikic @dtcxzyw @dianqk

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zyx-billy zyx-billy merged commit 69f1442 into llvm:main May 3, 2024
4 checks passed
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