llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Code explosion when LoopVectorize vectorizes a loop with a stride of 8 floats, part 2

Open rotateright opened this issue 10 years ago • 16 comments

Bugzilla Link 27881
Resolution FIXED
Resolved on Oct 13, 2019 18:38
Version trunk
OS All
CC @atrick,@ayalz,@delena,@fhahn,@hfinkel,@RKSimon

Extended Description

To show how fragile the fix for bug 27826 was, let's +1 that example:

float foo(float *a, float *b, int n) { if (n % 8 != 0) __builtin_unreachable();

float s = 0; for (int i=0; i<n; i+=8) s += (a[i] + b[i] + 1.0f); // one more fadd to tip the cost scale

return s; }


Or as IR input for 'opt -loop-vectorize':

target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0"

define float @​foo(float* %a, float* %b, i32 %n) { entry: %cmp113 = icmp sgt i32 %n, 0 br i1 %cmp113, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader: %t0 = sext i32 %n to i64 br label %for.body

for.cond.cleanup: %s.0.lcssa = phi float [ 0.0, %entry ], [ %add5, %for.body ] ret float %s.0.lcssa

for.body: %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ] %s = phi float [ 0.0, %for.body.preheader ], [ %add5, %for.body ] %arrayidx = getelementptr inbounds float, float* %a, i64 %indvars.iv %t1 = load float, float* %arrayidx, align 4 %arrayidx3 = getelementptr inbounds float, float* %b, i64 %indvars.iv %t2 = load float, float* %arrayidx3, align 4 %add = fadd fast float %s, 1.0 %add4 = fadd fast float %add, %t1 %add5 = fadd fast float %add4, %t2 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 8 %cmp1 = icmp slt i64 %indvars.iv.next, %t0 br i1 %cmp1, label %for.body, label %for.cond.cleanup }


Ready. Set. BOOM!

$ ./opt -loop-vectorize explode.ll -S -mattr=avx ; ModuleID = 'explode.ll' source_filename = "explode.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0"

define float @​foo(float* %a, float* %b, i32 %n) #​0 { entry: %cmp113 = icmp sgt i32 %n, 0 br i1 %cmp113, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader: ; preds = %entry %t0 = sext i32 %n to i64 %0 = add i64 %t0, -1 %1 = lshr i64 %0, 3 %2 = add i64 %1, 1 %min.iters.check = icmp ult i64 %2, 32 br i1 %min.iters.check, label %scalar.ph, label %min.iters.checked

min.iters.checked: ; preds = %for.body.preheader %n.mod.vf = urem i64 %2, 32 %n.vec = sub i64 %2, %n.mod.vf %cmp.zero = icmp eq i64 %n.vec, 0 %ind.end = shl i64 %n.vec, 3 br i1 %cmp.zero, label %scalar.ph, label %vector.ph

vector.ph: ; preds = %min.iters.checked %broadcast.splatinsert7 = insertelement <8 x i64> undef, i64 %t0, i32 0 %broadcast.splat8 = shufflevector <8 x i64> %broadcast.splatinsert7, <8 x i64> undef, <8 x i32> zeroinitializer br label %vector.body

vector.body: ; preds = %vector.body, %vector.ph %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ] %vec.phi = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %363, %vector.body ] %vec.phi4 = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %364, %vector.body ] %vec.phi5 = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %365, %vector.body ] %vec.phi6 = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %366, %vector.body ] %offset.idx = shl i64 %index, 3 %broadcast.splatinsert = insertelement <8 x i64> undef, i64 %offset.idx, i32 0 %broadcast.splat = shufflevector <8 x i64> %broadcast.splatinsert, <8 x i64> undef, <8 x i32> zeroinitializer %induction = add <8 x i64> %broadcast.splat, <i64 0, i64 8, i64 16, i64 24, i64 32, i64 40, i64 48, i64 56> %induction1 = add <8 x i64> %broadcast.splat, <i64 64, i64 72, i64 80, i64 88, i64 96, i64 104, i64 112, i64 120> %induction2 = add <8 x i64> %broadcast.splat, <i64 128, i64 136, i64 144, i64 152, i64 160, i64 168, i64 176, i64 184> %induction3 = add <8 x i64> %broadcast.splat, <i64 192, i64 200, i64 208, i64 216, i64 224, i64 232, i64 240, i64 248> %3 = extractelement <8 x i64> %induction, i32 0 %4 = getelementptr inbounds float, float* %a, i64 %3 %5 = insertelement <8 x float*> undef, float* %4, i32 0 %6 = extractelement <8 x i64> %induction, i32 1 %7 = getelementptr inbounds float, float* %a, i64 %6 %8 = insertelement <8 x float*> %5, float* %7, i32 1 %9 = extractelement <8 x i64> %induction, i32 2 %10 = getelementptr inbounds float, float* %a, i64 %9 %11 = insertelement <8 x float*> %8, float* %10, i32 2 %12 = extractelement <8 x i64> %induction, i32 3 %13 = getelementptr inbounds float, float* %a, i64 %12 %14 = insertelement <8 x float*> %11, float* %13, i32 3 %15 = extractelement <8 x i64> %induction, i32 4 %16 = getelementptr inbounds float, float* %a, i64 %15 %17 = insertelement <8 x float*> %14, float* %16, i32 4 %18 = extractelement <8 x i64> %induction, i32 5 %19 = getelementptr inbounds float, float* %a, i64 %18 %20 = insertelement <8 x float*> %17, float* %19, i32 5 %21 = extractelement <8 x i64> %induction, i32 6 %22 = getelementptr inbounds float, float* %a, i64 %21 %23 = insertelement <8 x float*> %20, float* %22, i32 6 %24 = extractelement <8 x i64> %induction, i32 7 %25 = getelementptr inbounds float, float* %a, i64 %24 %26 = insertelement <8 x float*> %23, float* %25, i32 7 %27 = extractelement <8 x i64> %induction1, i32 0 %28 = getelementptr inbounds float, float* %a, i64 %27 %29 = insertelement <8 x float*> undef, float* %28, i32 0 %30 = extractelement <8 x i64> %induction1, i32 1 %31 = getelementptr inbounds float, float* %a, i64 %30 %32 = insertelement <8 x float*> %29, float* %31, i32 1 %33 = extractelement <8 x i64> %induction1, i32 2 %34 = getelementptr inbounds float, float* %a, i64 %33 %35 = insertelement <8 x float*> %32, float* %34, i32 2 %36 = extractelement <8 x i64> %induction1, i32 3 %37 = getelementptr inbounds float, float* %a, i64 %36 %38 = insertelement <8 x float*> %35, float* %37, i32 3 %39 = extractelement <8 x i64> %induction1, i32 4 %40 = getelementptr inbounds float, float* %a, i64 %39 %41 = insertelement <8 x float*> %38, float* %40, i32 4 %42 = extractelement <8 x i64> %induction1, i32 5 %43 = getelementptr inbounds float, float* %a, i64 %42 %44 = insertelement <8 x float*> %41, float* %43, i32 5 %45 = extractelement <8 x i64> %induction1, i32 6 %46 = getelementptr inbounds float, float* %a, i64 %45 %47 = insertelement <8 x float*> %44, float* %46, i32 6 %48 = extractelement <8 x i64> %induction1, i32 7 %49 = getelementptr inbounds float, float* %a, i64 %48 %50 = insertelement <8 x float*> %47, float* %49, i32 7 %51 = extractelement <8 x i64> %induction2, i32 0 %52 = getelementptr inbounds float, float* %a, i64 %51 %53 = insertelement <8 x float*> undef, float* %52, i32 0 %54 = extractelement <8 x i64> %induction2, i32 1 %55 = getelementptr inbounds float, float* %a, i64 %54 %56 = insertelement <8 x float*> %53, float* %55, i32 1 %57 = extractelement <8 x i64> %induction2, i32 2 %58 = getelementptr inbounds float, float* %a, i64 %57 %59 = insertelement <8 x float*> %56, float* %58, i32 2 %60 = extractelement <8 x i64> %induction2, i32 3 %61 = getelementptr inbounds float, float* %a, i64 %60 %62 = insertelement <8 x float*> %59, float* %61, i32 3 %63 = extractelement <8 x i64> %induction2, i32 4 %64 = getelementptr inbounds float, float* %a, i64 %63 %65 = insertelement <8 x float*> %62, float* %64, i32 4 %66 = extractelement <8 x i64> %induction2, i32 5 %67 = getelementptr inbounds float, float* %a, i64 %66 %68 = insertelement <8 x float*> %65, float* %67, i32 5 %69 = extractelement <8 x i64> %induction2, i32 6 %70 = getelementptr inbounds float, float* %a, i64 %69 %71 = insertelement <8 x float*> %68, float* %70, i32 6 %72 = extractelement <8 x i64> %induction2, i32 7 %73 = getelementptr inbounds float, float* %a, i64 %72 %74 = insertelement <8 x float*> %71, float* %73, i32 7 %75 = extractelement <8 x i64> %induction3, i32 0 %76 = getelementptr inbounds float, float* %a, i64 %75 %77 = insertelement <8 x float*> undef, float* %76, i32 0 %78 = extractelement <8 x i64> %induction3, i32 1 %79 = getelementptr inbounds float, float* %a, i64 %78 %80 = insertelement <8 x float*> %77, float* %79, i32 1 %81 = extractelement <8 x i64> %induction3, i32 2 %82 = getelementptr inbounds float, float* %a, i64 %81 %83 = insertelement <8 x float*> %80, float* %82, i32 2 %84 = extractelement <8 x i64> %induction3, i32 3 %85 = getelementptr inbounds float, float* %a, i64 %84 %86 = insertelement <8 x float*> %83, float* %85, i32 3 %87 = extractelement <8 x i64> %induction3, i32 4 %88 = getelementptr inbounds float, float* %a, i64 %87 %89 = insertelement <8 x float*> %86, float* %88, i32 4 %90 = extractelement <8 x i64> %induction3, i32 5 %91 = getelementptr inbounds float, float* %a, i64 %90 %92 = insertelement <8 x float*> %89, float* %91, i32 5 %93 = extractelement <8 x i64> %induction3, i32 6 %94 = getelementptr inbounds float, float* %a, i64 %93 %95 = insertelement <8 x float*> %92, float* %94, i32 6 %96 = extractelement <8 x i64> %induction3, i32 7 %97 = getelementptr inbounds float, float* %a, i64 %96 %98 = insertelement <8 x float*> %95, float* %97, i32 7 %99 = extractelement <8 x float*> %26, i32 0 %100 = load float, float* %99, align 4 %101 = insertelement <8 x float> undef, float %100, i32 0 %102 = extractelement <8 x float*> %26, i32 1 %103 = load float, float* %102, align 4 %104 = insertelement <8 x float> %101, float %103, i32 1 %105 = extractelement <8 x float*> %26, i32 2 %106 = load float, float* %105, align 4 %107 = insertelement <8 x float> %104, float %106, i32 2 %108 = extractelement <8 x float*> %26, i32 3 %109 = load float, float* %108, align 4 %110 = insertelement <8 x float> %107, float %109, i32 3 %111 = extractelement <8 x float*> %26, i32 4 %112 = load float, float* %111, align 4 %113 = insertelement <8 x float> %110, float %112, i32 4 %114 = extractelement <8 x float*> %26, i32 5 %115 = load float, float* %114, align 4 %116 = insertelement <8 x float> %113, float %115, i32 5 %117 = extractelement <8 x float*> %26, i32 6 %118 = load float, float* %117, align 4 %119 = insertelement <8 x float> %116, float %118, i32 6 %120 = extractelement <8 x float*> %26, i32 7 %121 = load float, float* %120, align 4 %122 = insertelement <8 x float> %119, float %121, i32 7 %123 = extractelement <8 x float*> %50, i32 0 %124 = load float, float* %123, align 4 %125 = insertelement <8 x float> undef, float %124, i32 0 %126 = extractelement <8 x float*> %50, i32 1 %127 = load float, float* %126, align 4 %128 = insertelement <8 x float> %125, float %127, i32 1 %129 = extractelement <8 x float*> %50, i32 2 %130 = load float, float* %129, align 4 %131 = insertelement <8 x float> %128, float %130, i32 2 %132 = extractelement <8 x float*> %50, i32 3 %133 = load float, float* %132, align 4 %134 = insertelement <8 x float> %131, float %133, i32 3 %135 = extractelement <8 x float*> %50, i32 4 %136 = load float, float* %135, align 4 %137 = insertelement <8 x float> %134, float %136, i32 4 %138 = extractelement <8 x float*> %50, i32 5 %139 = load float, float* %138, align 4 %140 = insertelement <8 x float> %137, float %139, i32 5 %141 = extractelement <8 x float*> %50, i32 6 %142 = load float, float* %141, align 4 %143 = insertelement <8 x float> %140, float %142, i32 6 %144 = extractelement <8 x float*> %50, i32 7 %145 = load float, float* %144, align 4 %146 = insertelement <8 x float> %143, float %145, i32 7 %147 = extractelement <8 x float*> %74, i32 0 %148 = load float, float* %147, align 4 %149 = insertelement <8 x float> undef, float %148, i32 0 %150 = extractelement <8 x float*> %74, i32 1 %151 = load float, float* %150, align 4 %152 = insertelement <8 x float> %149, float %151, i32 1 %153 = extractelement <8 x float*> %74, i32 2 %154 = load float, float* %153, align 4 %155 = insertelement <8 x float> %152, float %154, i32 2 %156 = extractelement <8 x float*> %74, i32 3 %157 = load float, float* %156, align 4 %158 = insertelement <8 x float> %155, float %157, i32 3 %159 = extractelement <8 x float*> %74, i32 4 %160 = load float, float* %159, align 4 %161 = insertelement <8 x float> %158, float %160, i32 4 %162 = extractelement <8 x float*> %74, i32 5 %163 = load float, float* %162, align 4 %164 = insertelement <8 x float> %161, float %163, i32 5 %165 = extractelement <8 x float*> %74, i32 6 %166 = load float, float* %165, align 4 %167 = insertelement <8 x float> %164, float %166, i32 6 %168 = extractelement <8 x float*> %74, i32 7 %169 = load float, float* %168, align 4 %170 = insertelement <8 x float> %167, float %169, i32 7 %171 = extractelement <8 x float*> %98, i32 0 %172 = load float, float* %171, align 4 %173 = insertelement <8 x float> undef, float %172, i32 0 %174 = extractelement <8 x float*> %98, i32 1 %175 = load float, float* %174, align 4 %176 = insertelement <8 x float> %173, float %175, i32 1 %177 = extractelement <8 x float*> %98, i32 2 %178 = load float, float* %177, align 4 %179 = insertelement <8 x float> %176, float %178, i32 2 %180 = extractelement <8 x float*> %98, i32 3 %181 = load float, float* %180, align 4 %182 = insertelement <8 x float> %179, float %181, i32 3 %183 = extractelement <8 x float*> %98, i32 4 %184 = load float, float* %183, align 4 %185 = insertelement <8 x float> %182, float %184, i32 4 %186 = extractelement <8 x float*> %98, i32 5 %187 = load float, float* %186, align 4 %188 = insertelement <8 x float> %185, float %187, i32 5 %189 = extractelement <8 x float*> %98, i32 6 %190 = load float, float* %189, align 4 %191 = insertelement <8 x float> %188, float %190, i32 6 %192 = extractelement <8 x float*> %98, i32 7 %193 = load float, float* %192, align 4 %194 = insertelement <8 x float> %191, float %193, i32 7 %195 = getelementptr inbounds float, float* %b, i64 %3 %196 = insertelement <8 x float*> undef, float* %195, i32 0 %197 = getelementptr inbounds float, float* %b, i64 %6 %198 = insertelement <8 x float*> %196, float* %197, i32 1 %199 = getelementptr inbounds float, float* %b, i64 %9 %200 = insertelement <8 x float*> %198, float* %199, i32 2 %201 = getelementptr inbounds float, float* %b, i64 %12 %202 = insertelement <8 x float*> %200, float* %201, i32 3 %203 = getelementptr inbounds float, float* %b, i64 %15 %204 = insertelement <8 x float*> %202, float* %203, i32 4 %205 = getelementptr inbounds float, float* %b, i64 %18 %206 = insertelement <8 x float*> %204, float* %205, i32 5 %207 = getelementptr inbounds float, float* %b, i64 %21 %208 = insertelement <8 x float*> %206, float* %207, i32 6 %209 = getelementptr inbounds float, float* %b, i64 %24 %210 = insertelement <8 x float*> %208, float* %209, i32 7 %211 = getelementptr inbounds float, float* %b, i64 %27 %212 = insertelement <8 x float*> undef, float* %211, i32 0 %213 = getelementptr inbounds float, float* %b, i64 %30 %214 = insertelement <8 x float*> %212, float* %213, i32 1 %215 = getelementptr inbounds float, float* %b, i64 %33 %216 = insertelement <8 x float*> %214, float* %215, i32 2 %217 = getelementptr inbounds float, float* %b, i64 %36 %218 = insertelement <8 x float*> %216, float* %217, i32 3 %219 = getelementptr inbounds float, float* %b, i64 %39 %220 = insertelement <8 x float*> %218, float* %219, i32 4 %221 = getelementptr inbounds float, float* %b, i64 %42 %222 = insertelement <8 x float*> %220, float* %221, i32 5 %223 = getelementptr inbounds float, float* %b, i64 %45 %224 = insertelement <8 x float*> %222, float* %223, i32 6 %225 = getelementptr inbounds float, float* %b, i64 %48 %226 = insertelement <8 x float*> %224, float* %225, i32 7 %227 = getelementptr inbounds float, float* %b, i64 %51 %228 = insertelement <8 x float*> undef, float* %227, i32 0 %229 = getelementptr inbounds float, float* %b, i64 %54 %230 = insertelement <8 x float*> %228, float* %229, i32 1 %231 = getelementptr inbounds float, float* %b, i64 %57 %232 = insertelement <8 x float*> %230, float* %231, i32 2 %233 = getelementptr inbounds float, float* %b, i64 %60 %234 = insertelement <8 x float*> %232, float* %233, i32 3 %235 = getelementptr inbounds float, float* %b, i64 %63 %236 = insertelement <8 x float*> %234, float* %235, i32 4 %237 = getelementptr inbounds float, float* %b, i64 %66 %238 = insertelement <8 x float*> %236, float* %237, i32 5 %239 = getelementptr inbounds float, float* %b, i64 %69 %240 = insertelement <8 x float*> %238, float* %239, i32 6 %241 = getelementptr inbounds float, float* %b, i64 %72 %242 = insertelement <8 x float*> %240, float* %241, i32 7 %243 = getelementptr inbounds float, float* %b, i64 %75 %244 = insertelement <8 x float*> undef, float* %243, i32 0 %245 = getelementptr inbounds float, float* %b, i64 %78 %246 = insertelement <8 x float*> %244, float* %245, i32 1 %247 = getelementptr inbounds float, float* %b, i64 %81 %248 = insertelement <8 x float*> %246, float* %247, i32 2 %249 = getelementptr inbounds float, float* %b, i64 %84 %250 = insertelement <8 x float*> %248, float* %249, i32 3 %251 = getelementptr inbounds float, float* %b, i64 %87 %252 = insertelement <8 x float*> %250, float* %251, i32 4 %253 = getelementptr inbounds float, float* %b, i64 %90 %254 = insertelement <8 x float*> %252, float* %253, i32 5 %255 = getelementptr inbounds float, float* %b, i64 %93 %256 = insertelement <8 x float*> %254, float* %255, i32 6 %257 = getelementptr inbounds float, float* %b, i64 %96 %258 = insertelement <8 x float*> %256, float* %257, i32 7 %259 = extractelement <8 x float*> %210, i32 0 %260 = load float, float* %259, align 4 %261 = insertelement <8 x float> undef, float %260, i32 0 %262 = extractelement <8 x float*> %210, i32 1 %263 = load float, float* %262, align 4 %264 = insertelement <8 x float> %261, float %263, i32 1 %265 = extractelement <8 x float*> %210, i32 2 %266 = load float, float* %265, align 4 %267 = insertelement <8 x float> %264, float %266, i32 2 %268 = extractelement <8 x float*> %210, i32 3 %269 = load float, float* %268, align 4 %270 = insertelement <8 x float> %267, float %269, i32 3 %271 = extractelement <8 x float*> %210, i32 4 %272 = load float, float* %271, align 4 %273 = insertelement <8 x float> %270, float %272, i32 4 %274 = extractelement <8 x float*> %210, i32 5 %275 = load float, float* %274, align 4 %276 = insertelement <8 x float> %273, float %275, i32 5 %277 = extractelement <8 x float*> %210, i32 6 %278 = load float, float* %277, align 4 %279 = insertelement <8 x float> %276, float %278, i32 6 %280 = extractelement <8 x float*> %210, i32 7 %281 = load float, float* %280, align 4 %282 = insertelement <8 x float> %279, float %281, i32 7 %283 = extractelement <8 x float*> %226, i32 0 %284 = load float, float* %283, align 4 %285 = insertelement <8 x float> undef, float %284, i32 0 %286 = extractelement <8 x float*> %226, i32 1 %287 = load float, float* %286, align 4 %288 = insertelement <8 x float> %285, float %287, i32 1 %289 = extractelement <8 x float*> %226, i32 2 %290 = load float, float* %289, align 4 %291 = insertelement <8 x float> %288, float %290, i32 2 %292 = extractelement <8 x float*> %226, i32 3 %293 = load float, float* %292, align 4 %294 = insertelement <8 x float> %291, float %293, i32 3 %295 = extractelement <8 x float*> %226, i32 4 %296 = load float, float* %295, align 4 %297 = insertelement <8 x float> %294, float %296, i32 4 %298 = extractelement <8 x float*> %226, i32 5 %299 = load float, float* %298, align 4 %300 = insertelement <8 x float> %297, float %299, i32 5 %301 = extractelement <8 x float*> %226, i32 6 %302 = load float, float* %301, align 4 %303 = insertelement <8 x float> %300, float %302, i32 6 %304 = extractelement <8 x float*> %226, i32 7 %305 = load float, float* %304, align 4 %306 = insertelement <8 x float> %303, float %305, i32 7 %307 = extractelement <8 x float*> %242, i32 0 %308 = load float, float* %307, align 4 %309 = insertelement <8 x float> undef, float %308, i32 0 %310 = extractelement <8 x float*> %242, i32 1 %311 = load float, float* %310, align 4 %312 = insertelement <8 x float> %309, float %311, i32 1 %313 = extractelement <8 x float*> %242, i32 2 %314 = load float, float* %313, align 4 %315 = insertelement <8 x float> %312, float %314, i32 2 %316 = extractelement <8 x float*> %242, i32 3 %317 = load float, float* %316, align 4 %318 = insertelement <8 x float> %315, float %317, i32 3 %319 = extractelement <8 x float*> %242, i32 4 %320 = load float, float* %319, align 4 %321 = insertelement <8 x float> %318, float %320, i32 4 %322 = extractelement <8 x float*> %242, i32 5 %323 = load float, float* %322, align 4 %324 = insertelement <8 x float> %321, float %323, i32 5 %325 = extractelement <8 x float*> %242, i32 6 %326 = load float, float* %325, align 4 %327 = insertelement <8 x float> %324, float %326, i32 6 %328 = extractelement <8 x float*> %242, i32 7 %329 = load float, float* %328, align 4 %330 = insertelement <8 x float> %327, float %329, i32 7 %331 = extractelement <8 x float*> %258, i32 0 %332 = load float, float* %331, align 4 %333 = insertelement <8 x float> undef, float %332, i32 0 %334 = extractelement <8 x float*> %258, i32 1 %335 = load float, float* %334, align 4 %336 = insertelement <8 x float> %333, float %335, i32 1 %337 = extractelement <8 x float*> %258, i32 2 %338 = load float, float* %337, align 4 %339 = insertelement <8 x float> %336, float %338, i32 2 %340 = extractelement <8 x float*> %258, i32 3 %341 = load float, float* %340, align 4 %342 = insertelement <8 x float> %339, float %341, i32 3 %343 = extractelement <8 x float*> %258, i32 4 %344 = load float, float* %343, align 4 %345 = insertelement <8 x float> %342, float %344, i32 4 %346 = extractelement <8 x float*> %258, i32 5 %347 = load float, float* %346, align 4 %348 = insertelement <8 x float> %345, float %347, i32 5 %349 = extractelement <8 x float*> %258, i32 6 %350 = load float, float* %349, align 4 %351 = insertelement <8 x float> %348, float %350, i32 6 %352 = extractelement <8 x float*> %258, i32 7 %353 = load float, float* %352, align 4 %354 = insertelement <8 x float> %351, float %353, i32 7 %355 = fadd fast <8 x float> %vec.phi, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %356 = fadd fast <8 x float> %vec.phi4, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %357 = fadd fast <8 x float> %vec.phi5, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %358 = fadd fast <8 x float> %vec.phi6, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %359 = fadd fast <8 x float> %355, %122 %360 = fadd fast <8 x float> %356, %146 %361 = fadd fast <8 x float> %357, %170 %362 = fadd fast <8 x float> %358, %194 %363 = fadd fast <8 x float> %359, %282 %364 = fadd fast <8 x float> %360, %306 %365 = fadd fast <8 x float> %361, %330 %366 = fadd fast <8 x float> %362, %354 %367 = add nuw nsw <8 x i64> %induction, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %368 = add nuw nsw <8 x i64> %induction1, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %369 = add nuw nsw <8 x i64> %induction2, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %370 = add nuw nsw <8 x i64> %induction3, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %371 = icmp slt <8 x i64> %367, %broadcast.splat8 %372 = icmp slt <8 x i64> %368, %broadcast.splat8 %373 = icmp slt <8 x i64> %369, %broadcast.splat8 %374 = icmp slt <8 x i64> %370, %broadcast.splat8 %index.next = add i64 %index, 32 %375 = icmp eq i64 %index.next, %n.vec br i1 %375, label %middle.block, label %vector.body, !llvm.loop !​0

middle.block: ; preds = %vector.body %bin.rdx = fadd fast <8 x float> %364, %363 %bin.rdx9 = fadd fast <8 x float> %365, %bin.rdx %bin.rdx10 = fadd fast <8 x float> %366, %bin.rdx9 %rdx.shuf = shufflevector <8 x float> %bin.rdx10, <8 x float> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx11 = fadd fast <8 x float> %bin.rdx10, %rdx.shuf %rdx.shuf12 = shufflevector <8 x float> %bin.rdx11, <8 x float> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx13 = fadd fast <8 x float> %bin.rdx11, %rdx.shuf12 %rdx.shuf14 = shufflevector <8 x float> %bin.rdx13, <8 x float> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx15 = fadd fast <8 x float> %bin.rdx13, %rdx.shuf14 %376 = extractelement <8 x float> %bin.rdx15, i32 0 %cmp.n = icmp eq i64 %2, %n.vec br i1 %cmp.n, label %for.cond.cleanup.loopexit, label %scalar.ph

scalar.ph: ; preds = %middle.block, %min.iters.checked, %for.body.preheader %bc.resume.val = phi i64 [ %ind.end, %middle.block ], [ 0, %for.body.preheader ], [ 0, %min.iters.checked ] %bc.merge.rdx = phi float [ 0.000000e+00, %for.body.preheader ], [ 0.000000e+00, %min.iters.checked ], [ %376, %middle.block ] br label %for.body

for.cond.cleanup.loopexit: ; preds = %middle.block, %for.body %add5.lcssa = phi float [ %add5, %for.body ], [ %376, %middle.block ] br label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry %s.0.lcssa = phi float [ 0.000000e+00, %entry ], [ %add5.lcssa, %for.cond.cleanup.loopexit ] ret float %s.0.lcssa

for.body: ; preds = %for.body, %scalar.ph %indvars.iv = phi i64 [ %bc.resume.val, %scalar.ph ], [ %indvars.iv.next, %for.body ] %s = phi float [ %bc.merge.rdx, %scalar.ph ], [ %add5, %for.body ] %arrayidx = getelementptr inbounds float, float* %a, i64 %indvars.iv %t1 = load float, float* %arrayidx, align 4 %arrayidx3 = getelementptr inbounds float, float* %b, i64 %indvars.iv %t2 = load float, float* %arrayidx3, align 4 %add = fadd fast float %s, 1.000000e+00 %add4 = fadd fast float %add, %t1 %add5 = fadd fast float %add4, %t2 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 8 %cmp1 = icmp slt i64 %indvars.iv.next, %t0 br i1 %cmp1, label %for.body, label %for.cond.cleanup.loopexit, !llvm.loop !​3 }

attributes #​0 = { "target-features"="+avx" }

!​0 = distinct !{#0, !​1, !​2} !​1 = !{!"llvm.loop.vectorize.width", i32 1} !​2 = !{!"llvm.loop.interleave.count", i32 1} !​3 = distinct !{#3, !​4, !​1, !​2} !​4 = !{!"llvm.loop.unroll.runtime.disable"}

rotateright avatar May 25 '16 20:05 rotateright

Looks like this has been fixed in trunk. Please re-open if it is still an issue.

For the IR snippet, we now get the code below. The vectorised version seems to be around 3x faster on my X86 box.

source_filename = "tc2.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0"

define float @​foo(float* %a, float* %b, i32 %n) #​0 { entry: %cmp113 = icmp sgt i32 %n, 0 br i1 %cmp113, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader: ; preds = %entry %t0 = sext i32 %n to i64 %0 = add i64 %t0, -1 %1 = lshr i64 %0, 3 %2 = add nuw nsw i64 %1, 1 %min.iters.check = icmp ule i64 %2, 16 br i1 %min.iters.check, label %scalar.ph, label %vector.ph

vector.ph: ; preds = %for.body.preheader %n.mod.vf = urem i64 %2, 16 %3 = icmp eq i64 %n.mod.vf, 0 %4 = select i1 %3, i64 16, i64 %n.mod.vf %n.vec = sub i64 %2, %4 %ind.end = mul i64 %n.vec, 8 br label %vector.body

vector.body: ; preds = %vector.body, %vector.ph %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ] %vec.phi = phi <4 x float> [ zeroinitializer, %vector.ph ], [ %41, %vector.body ] %vec.phi4 = phi <4 x float> [ zeroinitializer, %vector.ph ], [ %42, %vector.body ] %vec.phi5 = phi <4 x float> [ zeroinitializer, %vector.ph ], [ %43, %vector.body ] %vec.phi6 = phi <4 x float> [ zeroinitializer, %vector.ph ], [ %44, %vector.body ] %offset.idx = mul i64 %index, 8 %broadcast.splatinsert = insertelement <4 x i64> undef, i64 %offset.idx, i32 0 %broadcast.splat = shufflevector <4 x i64> %broadcast.splatinsert, <4 x i64> undef, <4 x i32> zeroinitializer %induction = add <4 x i64> %broadcast.splat, <i64 0, i64 8, i64 16, i64 24> %induction1 = add <4 x i64> %broadcast.splat, <i64 32, i64 40, i64 48, i64 56> %induction2 = add <4 x i64> %broadcast.splat, <i64 64, i64 72, i64 80, i64 88> %induction3 = add <4 x i64> %broadcast.splat, <i64 96, i64 104, i64 112, i64 120> %5 = add i64 %offset.idx, 0 %6 = add i64 %offset.idx, 32 %7 = add i64 %offset.idx, 64 %8 = add i64 %offset.idx, 96 %9 = getelementptr inbounds float, float* %a, i64 %5 %10 = getelementptr inbounds float, float* %a, i64 %6 %11 = getelementptr inbounds float, float* %a, i64 %7 %12 = getelementptr inbounds float, float* %a, i64 %8 %13 = getelementptr inbounds float, float* %9, i32 0 %14 = bitcast float* %13 to <32 x float>* %15 = getelementptr inbounds float, float* %10, i32 0 %16 = bitcast float* %15 to <32 x float>* %17 = getelementptr inbounds float, float* %11, i32 0 %18 = bitcast float* %17 to <32 x float>* %19 = getelementptr inbounds float, float* %12, i32 0 %20 = bitcast float* %19 to <32 x float>* %wide.vec = load <32 x float>, <32 x float>* %14, align 4 %wide.vec7 = load <32 x float>, <32 x float>* %16, align 4 %wide.vec8 = load <32 x float>, <32 x float>* %18, align 4 %wide.vec9 = load <32 x float>, <32 x float>* %20, align 4 %strided.vec = shufflevector <32 x float> %wide.vec, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec10 = shufflevector <32 x float> %wide.vec7, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec11 = shufflevector <32 x float> %wide.vec8, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec12 = shufflevector <32 x float> %wide.vec9, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %21 = getelementptr inbounds float, float* %b, i64 %5 %22 = getelementptr inbounds float, float* %b, i64 %6 %23 = getelementptr inbounds float, float* %b, i64 %7 %24 = getelementptr inbounds float, float* %b, i64 %8 %25 = getelementptr inbounds float, float* %21, i32 0 %26 = bitcast float* %25 to <32 x float>* %27 = getelementptr inbounds float, float* %22, i32 0 %28 = bitcast float* %27 to <32 x float>* %29 = getelementptr inbounds float, float* %23, i32 0 %30 = bitcast float* %29 to <32 x float>* %31 = getelementptr inbounds float, float* %24, i32 0 %32 = bitcast float* %31 to <32 x float>* %wide.vec13 = load <32 x float>, <32 x float>* %26, align 4 %wide.vec14 = load <32 x float>, <32 x float>* %28, align 4 %wide.vec15 = load <32 x float>, <32 x float>* %30, align 4 %wide.vec16 = load <32 x float>, <32 x float>* %32, align 4 %strided.vec17 = shufflevector <32 x float> %wide.vec13, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec18 = shufflevector <32 x float> %wide.vec14, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec19 = shufflevector <32 x float> %wide.vec15, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %strided.vec20 = shufflevector <32 x float> %wide.vec16, <32 x float> undef, <4 x i32> <i32 0, i32 8, i32 16, i32 24> %33 = fadd fast <4 x float> %vec.phi, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %34 = fadd fast <4 x float> %vec.phi4, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %35 = fadd fast <4 x float> %vec.phi5, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %36 = fadd fast <4 x float> %vec.phi6, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %37 = fadd fast <4 x float> %33, %strided.vec %38 = fadd fast <4 x float> %34, %strided.vec10 %39 = fadd fast <4 x float> %35, %strided.vec11 %40 = fadd fast <4 x float> %36, %strided.vec12 %41 = fadd fast <4 x float> %37, %strided.vec17 %42 = fadd fast <4 x float> %38, %strided.vec18 %43 = fadd fast <4 x float> %39, %strided.vec19 %44 = fadd fast <4 x float> %40, %strided.vec20 %index.next = add i64 %index, 16 %45 = icmp eq i64 %index.next, %n.vec br i1 %45, label %middle.block, label %vector.body, !llvm.loop !​0

middle.block: ; preds = %vector.body %bin.rdx = fadd fast <4 x float> %42, %41 %bin.rdx21 = fadd fast <4 x float> %43, %bin.rdx %bin.rdx22 = fadd fast <4 x float> %44, %bin.rdx21 %rdx.shuf = shufflevector <4 x float> %bin.rdx22, <4 x float> undef, <4 x i32> <i32 2, i32 3, i32 undef, i32 undef> %bin.rdx23 = fadd fast <4 x float> %bin.rdx22, %rdx.shuf %rdx.shuf24 = shufflevector <4 x float> %bin.rdx23, <4 x float> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> %bin.rdx25 = fadd fast <4 x float> %bin.rdx23, %rdx.shuf24 %46 = extractelement <4 x float> %bin.rdx25, i32 0 %cmp.n = icmp eq i64 %2, %n.vec br i1 %cmp.n, label %for.cond.cleanup.loopexit, label %scalar.ph

scalar.ph: ; preds = %middle.block, %for.body.preheader %bc.resume.val = phi i64 [ %ind.end, %middle.block ], [ 0, %for.body.preheader ] %bc.merge.rdx = phi float [ 0.000000e+00, %for.body.preheader ], [ %46, %middle.block ] br label %for.body

for.cond.cleanup.loopexit: ; preds = %middle.block, %for.body %add5.lcssa = phi float [ %add5, %for.body ], [ %46, %middle.block ] br label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry %s.0.lcssa = phi float [ 0.000000e+00, %entry ], [ %add5.lcssa, %for.cond.cleanup.loopexit ] ret float %s.0.lcssa

for.body: ; preds = %for.body, %scalar.ph %indvars.iv = phi i64 [ %bc.resume.val, %scalar.ph ], [ %indvars.iv.next, %for.body ] %s = phi float [ %bc.merge.rdx, %scalar.ph ], [ %add5, %for.body ] %arrayidx = getelementptr inbounds float, float* %a, i64 %indvars.iv %t1 = load float, float* %arrayidx, align 4 %arrayidx3 = getelementptr inbounds float, float* %b, i64 %indvars.iv %t2 = load float, float* %arrayidx3, align 4 %add = fadd fast float %s, 1.000000e+00 %add4 = fadd fast float %add, %t1 %add5 = fadd fast float %add4, %t2 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 8 %cmp1 = icmp slt i64 %indvars.iv.next, %t0 br i1 %cmp1, label %for.body, label %for.cond.cleanup.loopexit, !llvm.loop !​2 }

attributes #​0 = { "target-features"="+avx" }

!​0 = distinct !{#0, !​1} !​1 = !{!"llvm.loop.isvectorized", i32 1} !​2 = distinct !{#2, !​3, !​1} !​3 = !{!"llvm.loop.unroll.runtime.disable"}

fhahn avatar Oct 14 '19 01:10 fhahn

The patch below should help somewhat with the code explosion in this example (pre-InstCombine), as it will prevent the vectorizer from generating the unneeded insertelement and extractelement instructions.

https://reviews.llvm.org/D23169

Nice! That kills over 100 instructions (~25%) in this example.

rotateright avatar Aug 05 '16 00:08 rotateright

The patch below should help somewhat with the code explosion in this example (pre-InstCombine), as it will prevent the vectorizer from generating the unneeded insertelement and extractelement instructions.

https://reviews.llvm.org/D23169

llvmbot avatar Aug 04 '16 20:08 llvmbot

I tried to vectorize GEP instruction and the generated code looks significantly shorter:

; ModuleID = '/tmp/foo.ll' source_filename = "/tmp/foo.ll" target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0"

define float @​foo(float* %a, float* %b, i32 %n) #​0 { entry: %cmp113 = icmp sgt i32 %n, 0 br i1 %cmp113, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader: ; preds = %entry %t0 = sext i32 %n to i64 %0 = add i64 %t0, -1 %1 = lshr i64 %0, 3 %2 = add i64 %1, 1 %min.iters.check = icmp ult i64 %2, 16 br i1 %min.iters.check, label %scalar.ph, label %min.iters.checked

min.iters.checked: ; preds = %for.body.preheader %n.mod.vf = urem i64 %2, 16 %n.vec = sub i64 %2, %n.mod.vf %cmp.zero = icmp eq i64 %n.vec, 0 %ind.end = shl i64 %n.vec, 3 br i1 %cmp.zero, label %scalar.ph, label %vector.ph

vector.ph: ; preds = %min.iters.checked %broadcast.splatinsert = insertelement <8 x i64> undef, i64 %t0, i32 0 %broadcast.splat = shufflevector <8 x i64> %broadcast.splatinsert, <8 x i64> undef, <8 x i32> zeroinitializer br label %vector.body

vector.body: ; preds = %vector.body, %vector.ph %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ] %vec.ind = phi <8 x i64> [ <i64 0, i64 8, i64 16, i64 24, i64 32, i64 40, i64 48, i64 56>, %vector.ph ], [ %step.add1, %vector.body ] %vec.phi = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %103, %vector.body ] %vec.phi2 = phi <8 x float> [ zeroinitializer, %vector.ph ], [ %104, %vector.body ] %step.add = add <8 x i64> %vec.ind, <i64 64, i64 64, i64 64, i64 64, i64 64, i64 64, i64 64, i64 64> %step.add1 = add <8 x i64> %step.add, <i64 64, i64 64, i64 64, i64 64, i64 64, i64 64, i64 64, i64 64> %VectorGep = getelementptr inbounds float, float* %a, <8 x i64> %vec.ind %VectorGep3 = getelementptr inbounds float, float* %a, <8 x i64> %step.add %3 = extractelement <8 x float*> %VectorGep, i32 0 %4 = load float, float* %3, align 4 %5 = insertelement <8 x float> undef, float %4, i32 0 %6 = extractelement <8 x float*> %VectorGep, i32 1 %7 = load float, float* %6, align 4 %8 = insertelement <8 x float> %5, float %7, i32 1 %9 = extractelement <8 x float*> %VectorGep, i32 2 %10 = load float, float* %9, align 4 %11 = insertelement <8 x float> %8, float %10, i32 2 %12 = extractelement <8 x float*> %VectorGep, i32 3 %13 = load float, float* %12, align 4 %14 = insertelement <8 x float> %11, float %13, i32 3 %15 = extractelement <8 x float*> %VectorGep, i32 4 %16 = load float, float* %15, align 4 %17 = insertelement <8 x float> %14, float %16, i32 4 %18 = extractelement <8 x float*> %VectorGep, i32 5 %19 = load float, float* %18, align 4 %20 = insertelement <8 x float> %17, float %19, i32 5 %21 = extractelement <8 x float*> %VectorGep, i32 6 %22 = load float, float* %21, align 4 %23 = insertelement <8 x float> %20, float %22, i32 6 %24 = extractelement <8 x float*> %VectorGep, i32 7 %25 = load float, float* %24, align 4 %26 = insertelement <8 x float> %23, float %25, i32 7 %27 = extractelement <8 x float*> %VectorGep3, i32 0 %28 = load float, float* %27, align 4 %29 = insertelement <8 x float> undef, float %28, i32 0 %30 = extractelement <8 x float*> %VectorGep3, i32 1 %31 = load float, float* %30, align 4 %32 = insertelement <8 x float> %29, float %31, i32 1 %33 = extractelement <8 x float*> %VectorGep3, i32 2 %34 = load float, float* %33, align 4 %35 = insertelement <8 x float> %32, float %34, i32 2 %36 = extractelement <8 x float*> %VectorGep3, i32 3 %37 = load float, float* %36, align 4 %38 = insertelement <8 x float> %35, float %37, i32 3 %39 = extractelement <8 x float*> %VectorGep3, i32 4 %40 = load float, float* %39, align 4 %41 = insertelement <8 x float> %38, float %40, i32 4 %42 = extractelement <8 x float*> %VectorGep3, i32 5 %43 = load float, float* %42, align 4 %44 = insertelement <8 x float> %41, float %43, i32 5 %45 = extractelement <8 x float*> %VectorGep3, i32 6 %46 = load float, float* %45, align 4 %47 = insertelement <8 x float> %44, float %46, i32 6 %48 = extractelement <8 x float*> %VectorGep3, i32 7 %49 = load float, float* %48, align 4 %50 = insertelement <8 x float> %47, float %49, i32 7 %VectorGep4 = getelementptr inbounds float, float* %b, <8 x i64> %vec.ind %VectorGep5 = getelementptr inbounds float, float* %b, <8 x i64> %step.add %51 = extractelement <8 x float*> %VectorGep4, i32 0 %52 = load float, float* %51, align 4 %53 = insertelement <8 x float> undef, float %52, i32 0 %54 = extractelement <8 x float*> %VectorGep4, i32 1 %55 = load float, float* %54, align 4 %56 = insertelement <8 x float> %53, float %55, i32 1 %57 = extractelement <8 x float*> %VectorGep4, i32 2 %58 = load float, float* %57, align 4 %59 = insertelement <8 x float> %56, float %58, i32 2 %60 = extractelement <8 x float*> %VectorGep4, i32 3 %61 = load float, float* %60, align 4 %62 = insertelement <8 x float> %59, float %61, i32 3 %63 = extractelement <8 x float*> %VectorGep4, i32 4 %64 = load float, float* %63, align 4 %65 = insertelement <8 x float> %62, float %64, i32 4 %66 = extractelement <8 x float*> %VectorGep4, i32 5 %67 = load float, float* %66, align 4 %68 = insertelement <8 x float> %65, float %67, i32 5 %69 = extractelement <8 x float*> %VectorGep4, i32 6 %70 = load float, float* %69, align 4 %71 = insertelement <8 x float> %68, float %70, i32 6 %72 = extractelement <8 x float*> %VectorGep4, i32 7 %73 = load float, float* %72, align 4 %74 = insertelement <8 x float> %71, float %73, i32 7 %75 = extractelement <8 x float*> %VectorGep5, i32 0 %76 = load float, float* %75, align 4 %77 = insertelement <8 x float> undef, float %76, i32 0 %78 = extractelement <8 x float*> %VectorGep5, i32 1 %79 = load float, float* %78, align 4 %80 = insertelement <8 x float> %77, float %79, i32 1 %81 = extractelement <8 x float*> %VectorGep5, i32 2 %82 = load float, float* %81, align 4 %83 = insertelement <8 x float> %80, float %82, i32 2 %84 = extractelement <8 x float*> %VectorGep5, i32 3 %85 = load float, float* %84, align 4 %86 = insertelement <8 x float> %83, float %85, i32 3 %87 = extractelement <8 x float*> %VectorGep5, i32 4 %88 = load float, float* %87, align 4 %89 = insertelement <8 x float> %86, float %88, i32 4 %90 = extractelement <8 x float*> %VectorGep5, i32 5 %91 = load float, float* %90, align 4 %92 = insertelement <8 x float> %89, float %91, i32 5 %93 = extractelement <8 x float*> %VectorGep5, i32 6 %94 = load float, float* %93, align 4 %95 = insertelement <8 x float> %92, float %94, i32 6 %96 = extractelement <8 x float*> %VectorGep5, i32 7 %97 = load float, float* %96, align 4 %98 = insertelement <8 x float> %95, float %97, i32 7 %99 = fadd fast <8 x float> %vec.phi, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %100 = fadd fast <8 x float> %vec.phi2, <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00> %101 = fadd fast <8 x float> %99, %26 %102 = fadd fast <8 x float> %100, %50 %103 = fadd fast <8 x float> %101, %74 %104 = fadd fast <8 x float> %102, %98 %105 = add nuw nsw <8 x i64> %vec.ind, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %106 = add nuw nsw <8 x i64> %step.add, <i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8, i64 8> %107 = icmp slt <8 x i64> %105, %broadcast.splat %108 = icmp slt <8 x i64> %106, %broadcast.splat %index.next = add i64 %index, 16 %109 = icmp eq i64 %index.next, %n.vec br i1 %109, label %middle.block, label %vector.body, !llvm.loop !​0

middle.block: ; preds = %vector.body %bin.rdx = fadd fast <8 x float> %104, %103 %rdx.shuf = shufflevector <8 x float> %bin.rdx, <8 x float> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx6 = fadd fast <8 x float> %bin.rdx, %rdx.shuf %rdx.shuf7 = shufflevector <8 x float> %bin.rdx6, <8 x float> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx8 = fadd fast <8 x float> %bin.rdx6, %rdx.shuf7 %rdx.shuf9 = shufflevector <8 x float> %bin.rdx8, <8 x float> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef> %bin.rdx10 = fadd fast <8 x float> %bin.rdx8, %rdx.shuf9 %110 = extractelement <8 x float> %bin.rdx10, i32 0 %cmp.n = icmp eq i64 %2, %n.vec br i1 %cmp.n, label %for.cond.cleanup.loopexit, label %scalar.ph

scalar.ph: ; preds = %middle.block, %min.iters.checked, %for.body.preheader %bc.resume.val = phi i64 [ %ind.end, %middle.block ], [ 0, %for.body.preheader ], [ 0, %min.iters.checked ] %bc.merge.rdx = phi float [ 0.000000e+00, %for.body.preheader ], [ 0.000000e+00, %min.iters.checked ], [ %110, %middle.block ] br label %for.body

for.cond.cleanup.loopexit: ; preds = %middle.block, %for.body %add5.lcssa = phi float [ %add5, %for.body ], [ %110, %middle.block ] br label %for.cond.cleanup

for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry %s.0.lcssa = phi float [ 0.000000e+00, %entry ], [ %add5.lcssa, %for.cond.cleanup.loopexit ] ret float %s.0.lcssa

for.body: ; preds = %for.body, %scalar.ph %indvars.iv = phi i64 [ %bc.resume.val, %scalar.ph ], [ %indvars.iv.next, %for.body ] %s = phi float [ %bc.merge.rdx, %scalar.ph ], [ %add5, %for.body ] %arrayidx = getelementptr inbounds float, float* %a, i64 %indvars.iv %t1 = load float, float* %arrayidx, align 4 %arrayidx3 = getelementptr inbounds float, float* %b, i64 %indvars.iv %t2 = load float, float* %arrayidx3, align 4 %add = fadd fast float %s, 1.000000e+00 %add4 = fadd fast float %add, %t1 %add5 = fadd fast float %add4, %t2 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 8 %cmp1 = icmp slt i64 %indvars.iv.next, %t0 br i1 %cmp1, label %for.body, label %for.cond.cleanup.loopexit, !llvm.loop !​3 }

attributes #​0 = { "target-cpu"="corei7-avx" }

!​0 = distinct !{#0, !​1, !​2} !​1 = !{!"llvm.loop.vectorize.width", i32 1} !​2 = !{!"llvm.loop.interleave.count", i32 1} !​3 = distinct !{#3, !​4, !​1, !​2} !​4 = !{!"llvm.loop.unroll.runtime.disable"}

delena avatar Jul 07 '16 11:07 delena

Ideally, we'll still fix the vectorizer and/or its cost model to be more careful about vectorizing in this case.

I agree that in this particular case we shouldn't probably vectorize.

That said it should be possible to create a testcase with more vectorizable ops that would outweigh the cost of the scalar gathers. In this case we want the scalar pointer arithmetic as implement in Matt's change.

llvmbot avatar Jul 06 '16 18:07 llvmbot

That's right, Sanjay. The bigger issue here is the cost model.

llvmbot avatar Jul 06 '16 18:07 llvmbot

Hi Everyone,

I recently committed r274627, which allows the vectorizer to scalarize the step vectors in this example. This improves the pointer arithmetic sequence Adam commented on post-instcombine. The patch includes the example here as a test case.

Thanks, Matthew.

This is all noted in the patch review and commit message: but for the record, your commit causes the IR to become even bigger in this example (looks like another ~50 instructions) post-LV because the induction adds are done as scalar ops, but then InstCombine can zap a nice chunk of that, and the IR shrinks back down.

Ideally, we'll still fix the vectorizer and/or its cost model to be more careful about vectorizing in this case.

rotateright avatar Jul 06 '16 18:07 rotateright

Hi Everyone,

I recently committed r274627, which allows the vectorizer to scalarize the step vectors in this example. This improves the pointer arithmetic sequence Adam commented on post-instcombine. The patch includes the example here as a test case.

llvmbot avatar Jul 06 '16 17:07 llvmbot

Cost calculation should prevent vectorization in this case.

delena avatar May 26 '16 21:05 delena

@​Sanjay, I'm not sure if you're looking at this part as well but I was handed a similar case as yours and I've been looking at the pointer arithmetic sequence generated. Currently we derive the pointers for the scalarized loads from the step vector. This means that we build vectors just to perform a vector add and then extract all elements from the resulting vector afterwards. I have been working on a patch to teach instcombine to fully scalarize this sequence.

Hi Adam -

Thanks for letting me know. Yes, that seems like it could solve this or at least a big chunk of this.

Am I understanding correctly though that your patch would trigger after the loop vectorizer has created this mass of IR? I wonder if we can prevent the scalar->vector->scalar ops in the vectorizer itself?

rotateright avatar May 26 '16 20:05 rotateright

@​Sanjay, I'm not sure if you're looking at this part as well but I was handed a similar case as yours and I've been looking at the pointer arithmetic sequence generated. Currently we derive the pointers for the scalarized loads from the step vector. This means that we build vectors just to perform a vector add and then extract all elements from the resulting vector afterwards. I have been working on a patch to teach instcombine to fully scalarize this sequence.

llvmbot avatar May 26 '16 20:05 llvmbot

Yes, although just because the stride is more than 1 cache line does not necessarily mean that it will be memory bound. Stride-1 can also be memory bound ;) - it is really a function of the reuse and the overall size of the range.

Good point. It might be that the solution I'm looking for is already in the vectorizer, and I just don't know it. :)

I just noticed that PPC+Altivec doesn't vectorize this loop. That's because it's calculating a huge cost (32) for each load because it sees them as "interleaved" and: // For an interleaved access, calculate the total cost of the whole // interleave group.

It seems like this might be what I was trying to express - model each load's cost as the sum of all pieces in the stride even though we're not using 7/8ths of them?

rotateright avatar May 25 '16 23:05 rotateright

Exactly what do you plan on modeling? The loop needs to load the same data anyway. On an OOO core, you generally want to get many of these loads in flight so that we can execute the dependent instructions as the data comes in. The vectorizer's interleaving should actually be helpful here. Introducing dependencies between loaded results by shuffling them together might be harmful.

I was thinking that we'd add some large 'sparse memory penalty' to the cost of vectorizing a loop like this because there's likely no advantage to vectorizing something that is memory-bound.

Yes, although just because the stride is more than 1 cache line does not necessarily mean that it will be memory bound. Stride-1 can also be memory bound ;) - it is really a function of the reuse and the overall size of the range.

I'm not denying that there are other problems exposed by this example, but I think we already have something like this for address calcs in isLikelyComplexAddressComputation():

unsigned MaxMergeDistance = 64; if (StepVal > MaxMergeDistance) --> we're probably not going to vectorize

hfinkel avatar May 25 '16 21:05 hfinkel

Exactly what do you plan on modeling? The loop needs to load the same data anyway. On an OOO core, you generally want to get many of these loads in flight so that we can execute the dependent instructions as the data comes in. The vectorizer's interleaving should actually be helpful here. Introducing dependencies between loaded results by shuffling them together might be harmful.

I was thinking that we'd add some large 'sparse memory penalty' to the cost of vectorizing a loop like this because there's likely no advantage to vectorizing something that is memory-bound.

I'm not denying that there are other problems exposed by this example, but I think we already have something like this for address calcs in isLikelyComplexAddressComputation():

unsigned MaxMergeDistance = 64; if (StepVal > MaxMergeDistance) --> we're probably not going to vectorize

rotateright avatar May 25 '16 21:05 rotateright

As I wrote in bug 27826, comment 9, I think we need a more general override for the vectorizer's cost model when we encounter a loop with sparse memory accesses (big stride) and no hardware gather/scatter support.

We can't accurately model the cost of memory access instructions that are likely straining the memory bus by loading cachelines and not doing much with that data.

cc'ing some people who have touched LoopVectorize.cpp to see what they think.

Exactly what do you plan on modeling? The loop needs to load the same data anyway. On an OOO core, you generally want to get many of these loads in flight so that we can execute the dependent instructions as the data comes in. The vectorizer's interleaving should actually be helpful here. Introducing dependencies between loaded results by shuffling them together might be harmful.

hfinkel avatar May 25 '16 20:05 hfinkel

As I wrote in bug 27826, comment 9, I think we need a more general override for the vectorizer's cost model when we encounter a loop with sparse memory accesses (big stride) and no hardware gather/scatter support.

We can't accurately model the cost of memory access instructions that are likely straining the memory bus by loading cachelines and not doing much with that data.

cc'ing some people who have touched LoopVectorize.cpp to see what they think.

rotateright avatar May 25 '16 20:05 rotateright