[llvm] [LLVM] BasicTTIImpl allow unknown type during legality checking (PR #89848)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 24 11:00:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Billy Zhu (zyx-billy)

<details>
<summary>Changes</summary>

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
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
+}

``````````

</details>


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


More information about the llvm-commits mailing list