[llvm] [FuncSpec] Enable SpecializeLiteralConstant by default (PR #113442)

Alexandros Lamprineas via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 08:25:21 PDT 2024


================
@@ -0,0 +1,147 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --include-generated-funcs --version 5
+; RUN: opt -passes="ipsccp<func-spec>" -force-specialization \
+; RUN:     -funcspec-max-iters=3 -S < %s | FileCheck %s
+
+; RUN: opt -passes="ipsccp<func-spec>" -force-specialization          \
+; RUN:     -funcspec-for-literal-constant=false -funcspec-max-iters=3 \
+; RUN:     -S < %s | FileCheck %s --check-prefix=NOLIT
+
+ at global_true = constant i1 true
+ at global_false = constant i1 false
+
+define i64 @main() {
+entry:
+  %binop1 = call ptr @select_binop(ptr @global_true)
+  %binop2 = call ptr @select_binop(ptr @global_false)
+
+  %c1 = call i64 @compute(ptr %binop1)
+  %c2 = call i64 @compute(ptr %binop2)
+  %add = add i64 %c1, %c2
+  ret i64 %add
+}
+
+define ptr @select_binop(ptr %flag) {
+  %flag.val = load i1, ptr %flag
+  %binop = select i1 %flag.val, ptr @plus, ptr @minus
+  ret ptr %binop
+}
+
+define internal i64 @compute(ptr %binop) {
+entry:
+  %res = call i64 %binop(i64 1, i64 1)
+  ret i64 %res
+}
+
+define internal i64 @plus(i64 %x) {
+entry:
+  %sum = add i64 %x, 1
+  ret i64 %sum
+}
+
+define internal i64 @minus(i64 %x) {
+entry:
+  %diff = sub i64 %x, 1
+  ret i64 %diff
+}
+; CHECK-LABEL: define i64 @main() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[BINOP1:%.*]] = call ptr @select_binop.specialized.1(ptr @global_true)
+; CHECK-NEXT:    [[BINOP2:%.*]] = call ptr @select_binop.specialized.2(ptr @global_false)
+; CHECK-NEXT:    [[C1:%.*]] = call i64 @compute.specialized.3(ptr @plus)
+; CHECK-NEXT:    [[C2:%.*]] = call i64 @compute.specialized.4(ptr @minus)
+; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[C1]], [[C2]]
----------------
labrinea wrote:

I meant that SCCP is designed such that first it "solves" (analyzes the program, caches lattice values to its data structures) and then it replaces uses of values which are known to be constant. Things happen in this order by design. If you replaced the users earlier without invalidating the internal data structures (like resetLatticeValue does) then they would contain stale data and crash. I was fighting with such bugs when I merged FuncSpec with SCCP.

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


More information about the llvm-commits mailing list