[llvm] [LV] Narrow truncated inductions in a VPlan transform (PR #220730)
Alexander Kornienko via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 5 03:38:39 PDT 2026
alexfh wrote:
Hi @VedantParanjape, after this commit we've noticed a serious compilation time regression when compiling an instantiation of this function - https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/internal/optimized/optimized_ops.h#L2672 in https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/lstm.cc#L2337.
Here a test case demonstrating the slowdown (from ~2 to ~3 seconds for the loop vectorizer pass): https://godbolt.org/z/TW5bab8zz
```
===-------------------------------------------------------------------------===
Pass execution timing report
===-------------------------------------------------------------------------===
Total Execution Time: 2.0381 seconds (2.0973 wall clock)
---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Name ---
2.0286 (100.0%) 0.0094 ( 99.9%) 2.0380 (100.0%) 2.0972 (100.0%) LoopVectorizePass
```
->
```
2.9366 (100.0%) 0.0072 (100.0%) 2.9439 (100.0%) 2.9843 (100.0%) LoopVectorizePass
```
And here a full analysis from Gemini:
---
This change (`6c95a9274885`) caused an ~80% compile-time regression in `LoopVectorizePass` (~525s → ~950s) when compiling [`tflite/kernels/lstm.cc`](https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/lstm.cc) from LiteRT (TFLite) for `aarch64-unknown-linux-gnu` with `-O2 -fsanitize=signed-integer-overflow,shift`.
### Source Location & Instantiation
- **Instantiation site**: [`optimized_ops::LstmCell<4>(...)` in `tflite::ops::builtin::lstm::basic::Eval` (`tflite/kernels/lstm.cc:2337`)](https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/lstm.cc#L2337)
- **Function template**: [`template <int StateIntegerBits> inline void LstmCell(...)` (`tflite/kernels/internal/optimized/optimized_ops.h:2672`)](https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/internal/optimized/optimized_ops.h#L2672) with `StateIntegerBits = 4`
- **Affected loop**: The scalar remainder loop [`for (; c < output_depth; ++c)` (`tflite/kernels/internal/optimized/optimized_ops.h:2876-2940`)](https://github.com/google-ai-edge/LiteRT/blob/main/tflite/kernels/internal/optimized/optimized_ops.h#L2876), which inlines five 16-bit fixed-point `gemmlowp` activations (3x `gemmlowp::logistic`, 2x `gemmlowp::tanh`), expanding into a deep chain of `select`/`icmp`/`trunc`/`sext`/`zext` instructions and saturating/clamping intrinsics (`@llvm.sadd.sat.i16`, `@llvm.smax.i64`, `@llvm.smin.i64`).
### Root Cause
When `LoopVectorizationPlanner::tryToBuildVPlan` builds the scalar VPlan (`VF = 1`, `VF.isScalar()`):
1. `makeMemOpWideningDecisions` and `makeScalarizationDecisions` return early for `VF.isScalar()`, after which `makeCallWideningDecisions` replaces all intrinsic calls in the loop (`@llvm.abs.i16`, `@llvm.sadd.sat.i16`, `@llvm.smax.i64`, `@llvm.smin.i64`) with `VPReplicateRecipe(IsSingleScalar = true)` because `CM.isUniformAfterVectorization(I, VF)` is `true` for scalar `VF = 1`.
2. Because all stores and clamping points in the loop feed into those `VPReplicateRecipe(IsSingleScalar = true)` recipes (whose `usesFirstLaneOnly()` returns `true`), calling `vputils::onlyFirstLaneUsed(const VPValue *Def)` at `VF = 1` never short-circuits to `false` on any path. Since `vputils::onlyFirstLaneUsed` (`VPlanUtils.cpp:34`) performs an unmemoized recursive DFS over `Def->users()`, it walks an exponential number of paths (`O(2^N)`) through the chained `VPInstruction` / `VPBlendRecipe` DAG.
3. Prior to `6c95a9274885`, `tryToBuildVPlan` (`LoopVectorize.cpp:6681`) already triggered this exponential walk once per cast instruction at `VF = 1`. `VPlanTransforms::narrowInductionTruncates` (`VPlanTransforms.cpp:5941`) added a second unguarded pass that runs at `VF = 1` and calls `vputils::onlyFirstLaneUsed(&VPI)` on every `Trunc` instruction *before* checking `getOptimizableIVOf(Op, PSE)`, increasing `LoopVectorizePass` time by ~50–80%.
Checking `getOptimizableIVOf(Op, PSE)` before `vputils::onlyFirstLaneUsed(&VPI)` in `narrowInductionTruncates` (and/or skipping `narrowInductionTruncates` when `VF.isScalar()`) avoids the regression from `#220730`, and memoizing `vputils::onlyFirstLaneUsed` with a `SmallPtrSet<const VPValue *, 16> Visited` worklist (similar to `poisonGuaranteesUB` in `VPlanUtils.cpp`) eliminates the entire `O(2^N)` blowup (dropping `LoopVectorizePass` time from hundreds of seconds to `< 0.01s`).
### Reduced LLVM IR Reproducer (Single Fixed-Point `tanh` Stage)
Running `opt -passes=loop-vectorize -disable-output -time-passes` on the reduced loop below (1 of the 5 fixed-point activations from `LstmCell<4>`) takes **~2.0s** at `b47a631eeaff` vs **~3.0s** (+46%) at `6c95a9274885` (and **< 0.005s** if `vputils::onlyFirstLaneUsed` is memoized):
<details>
<summary><code>repro_lstm_tail.ll</code></summary>
```llvm
; RUN: opt -passes=loop-vectorize -disable-output -time-passes %s
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "aarch64-unknown-linux-gnu"
define void @repro_lstm_tail(ptr noalias %in_ptr, ptr noalias %out_ptr, i64 %n) {
entry:
br label %729
729:
%iv = phi i64 [ 0, %entry ], [ %next_iv, %1195 ]
%731 = getelementptr inbounds i16, ptr %in_ptr, i64 %iv
br label %976
976:
%977 = load i16, ptr %731, align 2
%978 = call i16 @llvm.abs.i16(i16 %977, i1 false)
%979 = sub i16 0, %978
%980 = or i16 %979, -512
%981 = shl nsw i16 %980, 4
%982 = add nsw i16 %981, 4096
%983 = sext i16 %982 to i32
%984 = mul nsw i32 %983, %983
%985 = add nuw nsw i32 %984, 16384
%986 = lshr i32 %985, 15
%987 = shl nuw i32 %986, 16
%988 = ashr exact i32 %987, 16
%989 = mul nsw i32 %988, %983
%990 = icmp sgt i32 %989, -1
%991 = select i1 %990, i32 16384, i32 -16383
%992 = add nsw i32 %991, %989
%993 = sdiv i32 %992, 32768
%994 = trunc nsw i32 %993 to i16
%995 = icmp eq i32 %986, 32768
%996 = mul nsw i32 %988, %988
%997 = add nuw nsw i32 %996, 16384
%998 = lshr i32 %997, 15
%999 = trunc nuw i32 %998 to i16
%1000 = select i1 %995, i16 32767, i16 %999
%1001 = lshr i16 %1000, 15
%1002 = add nuw nsw i16 %1001, 1
%1003 = and i16 %1000, 3
%1004 = ashr i16 %1000, 2
%1005 = icmp samesign ugt i16 %1003, %1002
%1006 = zext i1 %1005 to i16
%1007 = add nsw i16 %1004, %994
%1008 = add nsw i16 %1007, %1006
%1009 = icmp eq i16 %1008, 10923
br i1 %1009, label %1018, label %1010
1010:
%1011 = sext i16 %1008 to i32
%1012 = mul nsw i32 %1011, 10923
%1013 = icmp sgt i16 %1008, -1
%1014 = select i1 %1013, i32 16384, i32 -16383
%1015 = add nsw i32 %1014, %1012
%1016 = sdiv i32 %1015, 32768
%1017 = trunc nsw i32 %1016 to i16
br label %1018
1018:
%1019 = phi i16 [ %1017, %1010 ], [ 3641, %976 ]
%1020 = trunc nuw i32 %986 to i16
%1021 = add i16 %1019, %1020
%1022 = ashr i16 %1021, 1
%1023 = add nsw i16 %1022, %982
%1024 = and i16 %1021, 1
%1025 = lshr i16 %1021, 15
%1026 = icmp samesign ugt i16 %1024, %1025
%1027 = zext i1 %1026 to i16
%1028 = add nsw i16 %1023, %1027
%1029 = sext i16 %1028 to i32
%1030 = mul nsw i32 %1029, 28918
%1031 = icmp sgt i16 %1028, -1
%1032 = select i1 %1031, i32 16384, i32 -16383
%1033 = add nsw i32 %1032, %1030
%1034 = sdiv i32 %1033, 32768
%1035 = trunc nsw i32 %1034 to i16
%1036 = call i16 @llvm.sadd.sat.i16(i16 %1035, i16 28918)
%1037 = icmp eq i16 %1036, 25520
%1038 = zext nneg i16 %1036 to i32
%1039 = mul nuw nsw i32 %1038, 25520
%1040 = add nuw nsw i32 %1039, 16384
%1041 = lshr i32 %1040, 15
%1042 = trunc nuw nsw i32 %1041 to i16
%1043 = select i1 %1037, i16 19875, i16 %1042
%1044 = add i16 %980, %978
%1045 = and i16 %1044, 512
%1046 = icmp eq i16 %1045, 0
%1047 = select i1 %1046, i16 %1036, i16 %1043
%1048 = icmp eq i16 %1047, 19875
%1049 = zext nneg i16 %1047 to i32
%1050 = mul nuw nsw i32 %1049, 19875
%1051 = add nuw nsw i32 %1050, 16384
%1052 = lshr i32 %1051, 15
%1053 = trunc nuw nsw i32 %1052 to i16
%1054 = select i1 %1048, i16 12055, i16 %1053
%1055 = and i16 %1044, 1024
%1056 = icmp eq i16 %1055, 0
%1057 = select i1 %1056, i16 %1047, i16 %1054
%1058 = icmp eq i16 %1057, 12055
%1059 = zext nneg i16 %1057 to i32
%1060 = mul nuw nsw i32 %1059, 12055
%1061 = add nuw nsw i32 %1060, 16384
%1062 = lshr i32 %1061, 15
%1063 = trunc nuw nsw i32 %1062 to i16
%1064 = select i1 %1058, i16 4435, i16 %1063
%1065 = and i16 %1044, 2048
%1066 = icmp eq i16 %1065, 0
%1067 = select i1 %1066, i16 %1057, i16 %1064
%1068 = icmp eq i16 %1067, 4435
%1069 = zext nneg i16 %1067 to i32
%1070 = mul nuw nsw i32 %1069, 4435
%1071 = add nuw nsw i32 %1070, 16384
%1072 = lshr i32 %1071, 15
%1073 = trunc nuw nsw i32 %1072 to i16
%1074 = select i1 %1068, i16 600, i16 %1073
%1075 = and i16 %1044, 4096
%1076 = icmp eq i16 %1075, 0
%1077 = select i1 %1076, i16 %1067, i16 %1074
%1078 = icmp eq i16 %1077, 600
%1079 = zext nneg i16 %1077 to i32
%1080 = mul nuw nsw i32 %1079, 600
%1081 = add nuw nsw i32 %1080, 16384
%1082 = lshr i32 %1081, 15
%1083 = trunc nuw nsw i32 %1082 to i16
%1084 = select i1 %1078, i16 11, i16 %1083
%1085 = and i16 %1044, 8192
%1086 = icmp eq i16 %1085, 0
%1087 = select i1 %1086, i16 %1077, i16 %1084
%1088 = icmp ne i16 %1087, 11
%1089 = zext nneg i16 %1087 to i32
%1090 = mul nuw nsw i32 %1089, 11
%1091 = add nuw nsw i32 %1090, 16384
%1092 = lshr i32 %1091, 15
%1093 = trunc nuw nsw i32 %1092 to i16
%1094 = and i16 %1044, 16384
%1095 = icmp ne i16 %1094, 0
%1096 = select i1 %1095, i1 %1088, i1 false
%1097 = select i1 %1096, i16 %1093, i16 0
%1098 = select i1 %1095, i16 0, i16 %1087
%1099 = or disjoint i16 %1097, %1098
%1100 = icmp eq i16 %977, 0
%1101 = lshr i16 %1099, 1
%1102 = or i16 %1101, 16384
%1103 = select i1 %1100, i16 32767, i16 %1102
%1104 = zext nneg i16 %1103 to i32
%1105 = mul nuw nsw i32 %1104, 15420
%1106 = add nuw nsw i32 %1105, 16380
%1107 = lshr i32 %1106, 15
%1108 = trunc nuw nsw i32 %1107 to i16
%1109 = sub nuw nsw i16 23130, %1108
%1110 = zext nneg i16 %1109 to i32
%1111 = shl nuw nsw i32 %1104, 1
%1112 = mul nuw nsw i32 %1111, %1110
%1113 = add nuw nsw i32 %1112, 32768
%1114 = and i32 %1113, 2147418112
%1115 = sub nsw i32 536870912, %1114
%1116 = ashr exact i32 %1115, 16
%1117 = mul nsw i32 %1116, %1110
%1118 = icmp sgt i32 %1117, -1
%1119 = select i1 %1118, i32 16384, i32 -16383
%1120 = add nsw i32 %1119, %1117
%1121 = sdiv i32 %1120, 32768
%1122 = and i32 %1121, 65535
%1123 = zext nneg i32 %1122 to i64
%1124 = shl nuw i64 %1123, 48
%1125 = ashr exact i64 %1124, 46
%1126 = call i64 @llvm.smax.i64(i64 %1125, i64 -32768)
%1127 = call i64 @llvm.smin.i64(i64 %1126, i64 32767)
%1128 = trunc nsw i64 %1127 to i16
%1129 = add i16 %1109, %1128
%1130 = sext i16 %1129 to i32
%1131 = mul nsw i32 %1130, %1104
%1132 = icmp sgt i32 %1131, -1
%1133 = select i1 %1132, i32 16384, i32 -16383
%1134 = add nsw i32 %1133, %1131
%1135 = sdiv i32 %1134, -32768
%1136 = trunc nsw i32 %1135 to i16
%1137 = add i16 %1136, 8192
%1138 = sext i16 %1137 to i32
%1139 = icmp eq i16 %1129, %1137
br i1 %1139, label %1147, label %1140
1140:
%1141 = mul nsw i32 %1138, %1130
%1142 = icmp sgt i32 %1141, -1
%1143 = select i1 %1142, i32 16384, i32 -16383
%1144 = add nsw i32 %1143, %1141
%1145 = sdiv i32 %1144, 32768
%1146 = trunc i32 %1145 to i16
br label %1156
1147:
%1148 = icmp eq i16 %1129, -32768
%1149 = mul nsw i32 %1138, %1130
%1150 = icmp sgt i32 %1149, -1
%1151 = select i1 %1150, i32 16384, i32 -16383
%1152 = add nsw i32 %1151, %1149
%1153 = sdiv i32 %1152, 32768
%1154 = trunc i32 %1153 to i16
%1155 = select i1 %1148, i16 32767, i16 %1154
br label %1156
1156:
%1157 = phi i16 [ %1146, %1140 ], [ %1155, %1147 ]
%1158 = zext i16 %1157 to i64
%1159 = icmp slt i16 %1157, 8192
%1160 = icmp sgt i16 %1157, -8192
%1161 = shl nuw i64 %1158, 48
%1162 = ashr exact i64 %1161, 46
%1163 = call i64 @llvm.smax.i64(i64 %1162, i64 -32768)
%1164 = call i64 @llvm.smin.i64(i64 %1163, i64 32767)
%1165 = trunc nsw i64 %1164 to i16
%1166 = select i1 %1159, i16 %1165, i16 32767
%1167 = select i1 %1160, i16 %1166, i16 -32768
%1168 = add i16 %1167, %1129
%1169 = sext i16 %1168 to i32
%1170 = mul nsw i32 %1169, %1104
%1171 = icmp sgt i32 %1170, -1
%1172 = select i1 %1171, i32 16384, i32 -16383
%1173 = add nsw i32 %1172, %1170
%1174 = sdiv i32 %1173, -32768
%1175 = trunc nsw i32 %1174 to i16
%1176 = add i16 %1175, 8192
%1177 = sext i16 %1176 to i32
%1178 = icmp eq i16 %1168, %1176
br i1 %1178, label %1186, label %1179
1179:
%1180 = mul nsw i32 %1177, %1169
%1181 = icmp sgt i32 %1180, -1
%1182 = select i1 %1181, i32 16384, i32 -16383
%1183 = add nsw i32 %1182, %1180
%1184 = sdiv i32 %1183, 32768
%1185 = trunc i32 %1184 to i16
br label %1195
1186:
%1187 = icmp eq i16 %1168, -32768
%1188 = mul nsw i32 %1177, %1169
%1189 = icmp sgt i32 %1188, -1
%1190 = select i1 %1189, i32 16384, i32 -16383
%1191 = add nsw i32 %1190, %1188
%1192 = sdiv i32 %1191, 32768
%1193 = trunc i32 %1192 to i16
%1194 = select i1 %1187, i16 32767, i16 %1193
br label %1195
1195:
%1196 = phi i16 [ %1185, %1179 ], [ %1194, %1186 ]
%1197 = zext i16 %1196 to i64
%1198 = icmp slt i16 %1196, 8192
%1199 = icmp sgt i16 %1196, -8192
%1200 = shl nuw i64 %1197, 48
%1201 = ashr exact i64 %1200, 46
%1202 = call i64 @llvm.smax.i64(i64 %1201, i64 -32768)
%1203 = call i64 @llvm.smin.i64(i64 %1202, i64 32767)
%1204 = trunc nsw i64 %1203 to i16
%1205 = select i1 %1198, i16 %1204, i16 32767
%1206 = select i1 %1199, i16 %1205, i16 -32768
%1207 = add i16 %1168, -8192
%1208 = add i16 %1207, %1206
%1209 = zext i16 %1208 to i64
%1210 = icmp slt i16 %1208, 8192
%1211 = icmp sgt i16 %1208, -8192
%1212 = shl nuw i64 %1209, 48
%1213 = ashr exact i64 %1212, 46
%1214 = call i64 @llvm.smax.i64(i64 %1213, i64 -32768)
%1215 = call i64 @llvm.smin.i64(i64 %1214, i64 32767)
%1216 = trunc nsw i64 %1215 to i16
%1217 = select i1 %1210, i16 %1216, i16 32767
%1218 = select i1 %1211, i16 %1217, i16 -32768
%1219 = sub i16 0, %1218
%1220 = icmp slt i16 %977, 0
%1221 = select i1 %1220, i16 %1219, i16 %1218
%1222 = select i1 %1100, i16 0, i16 %1221
%res16 = call i16 @llvm.sadd.sat.i16(i16 %1222, i16 1)
%out_gep = getelementptr inbounds i16, ptr %out_ptr, i64 %iv
store i16 %res16, ptr %out_gep, align 2
%next_iv = add nuw nsw i64 %iv, 1
%cmp = icmp slt i64 %next_iv, %n
br i1 %cmp, label %729, label %exit
exit:
ret void
}
```
</details>
---
Please fix the compilation time regression. Thanks!
https://github.com/llvm/llvm-project/pull/220730
More information about the llvm-commits
mailing list