294 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 22; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -passes=separate-const-offset-from-gep,gvn \3; RUN: -reassociate-geps-verify-no-dead-code < %s \4; RUN: | FileCheck --check-prefix=IR %s5 6; Verifies the SeparateConstOffsetFromGEP pass.7; The following code computes8; *output = array[x][y] + array[x][y+1] + array[x+1][y] + array[x+1][y+1]9;10; We expect SeparateConstOffsetFromGEP to transform it to11;12; ptr base = &a[x][y];13; *output = base[0] + base[1] + base[32] + base[33];14;15; so the backend can emit PTX that uses fewer virtual registers.16 17@array = internal addrspace(3) global [32 x [32 x float]] zeroinitializer, align 418 19define void @sum_of_array(i32 %x, i32 %y, ptr nocapture %output) {20; IR-LABEL: define void @sum_of_array21; IR-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], ptr captures(none) [[OUTPUT:%.*]]) #[[ATTR0:[0-9]+]] {22; IR-NEXT: .preheader:23; IR-NEXT: [[I:%.*]] = sext i32 [[Y]] to i6424; IR-NEXT: [[I1:%.*]] = sext i32 [[X]] to i6425; IR-NEXT: [[I2:%.*]] = getelementptr [32 x [32 x float]], ptr addrspace(3) @array, i32 0, i32 [[X]], i32 [[Y]]26; IR-NEXT: [[I3:%.*]] = addrspacecast ptr addrspace(3) [[I2]] to ptr27; IR-NEXT: [[I4:%.*]] = load float, ptr [[I3]], align 428; IR-NEXT: [[I5:%.*]] = fadd float [[I4]], 0.000000e+0029; IR-NEXT: [[I87:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 430; IR-NEXT: [[I9:%.*]] = addrspacecast ptr addrspace(3) [[I87]] to ptr31; IR-NEXT: [[I10:%.*]] = load float, ptr [[I9]], align 432; IR-NEXT: [[I11:%.*]] = fadd float [[I5]], [[I10]]33; IR-NEXT: [[I1412:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 12834; IR-NEXT: [[I15:%.*]] = addrspacecast ptr addrspace(3) [[I1412]] to ptr35; IR-NEXT: [[I16:%.*]] = load float, ptr [[I15]], align 436; IR-NEXT: [[I17:%.*]] = fadd float [[I11]], [[I16]]37; IR-NEXT: [[I1818:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 13238; IR-NEXT: [[I19:%.*]] = addrspacecast ptr addrspace(3) [[I1818]] to ptr39; IR-NEXT: [[I20:%.*]] = load float, ptr [[I19]], align 440; IR-NEXT: [[I21:%.*]] = fadd float [[I17]], [[I20]]41; IR-NEXT: store float [[I21]], ptr [[OUTPUT]], align 442; IR-NEXT: ret void43;44.preheader:45 %i = sext i32 %y to i6446 %i1 = sext i32 %x to i6447 %i2 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i48 %i3 = addrspacecast ptr addrspace(3) %i2 to ptr49 %i4 = load float, ptr %i3, align 450 %i5 = fadd float %i4, 0.000000e+0051 %i6 = add i32 %y, 152 %i7 = sext i32 %i6 to i6453 %i8 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i754 %i9 = addrspacecast ptr addrspace(3) %i8 to ptr55 %i10 = load float, ptr %i9, align 456 %i11 = fadd float %i5, %i1057 %i12 = add i32 %x, 158 %i13 = sext i32 %i12 to i6459 %i14 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i13, i64 %i60 %i15 = addrspacecast ptr addrspace(3) %i14 to ptr61 %i16 = load float, ptr %i15, align 462 %i17 = fadd float %i11, %i1663 %i18 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i13, i64 %i764 %i19 = addrspacecast ptr addrspace(3) %i18 to ptr65 %i20 = load float, ptr %i19, align 466 %i21 = fadd float %i17, %i2067 store float %i21, ptr %output, align 468 ret void69}70 71 72; TODO: GVN is unable to preserve the "inbounds" keyword on the first GEP. Need73; some infrastructure changes to enable such optimizations.74 75; @sum_of_array2 is very similar to @sum_of_array. The only difference is in76; the order of "sext" and "add" when computing the array indices. @sum_of_array77; computes add before sext, e.g., array[sext(x + 1)][sext(y + 1)], while78; @sum_of_array2 computes sext before add,79; e.g., array[sext(x) + 1][sext(y) + 1]. SeparateConstOffsetFromGEP should be80; able to extract constant offsets from both forms.81define void @sum_of_array2(i32 %x, i32 %y, ptr nocapture %output) {82; IR-LABEL: define void @sum_of_array283; IR-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], ptr captures(none) [[OUTPUT:%.*]]) #[[ATTR0]] {84; IR-NEXT: .preheader:85; IR-NEXT: [[I:%.*]] = sext i32 [[Y]] to i6486; IR-NEXT: [[I1:%.*]] = sext i32 [[X]] to i6487; IR-NEXT: [[I2:%.*]] = getelementptr [32 x [32 x float]], ptr addrspace(3) @array, i32 0, i32 [[X]], i32 [[Y]]88; IR-NEXT: [[I3:%.*]] = addrspacecast ptr addrspace(3) [[I2]] to ptr89; IR-NEXT: [[I4:%.*]] = load float, ptr [[I3]], align 490; IR-NEXT: [[I5:%.*]] = fadd float [[I4]], 0.000000e+0091; IR-NEXT: [[I77:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 492; IR-NEXT: [[I8:%.*]] = addrspacecast ptr addrspace(3) [[I77]] to ptr93; IR-NEXT: [[I9:%.*]] = load float, ptr [[I8]], align 494; IR-NEXT: [[I10:%.*]] = fadd float [[I5]], [[I9]]95; IR-NEXT: [[I1212:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 12896; IR-NEXT: [[I13:%.*]] = addrspacecast ptr addrspace(3) [[I1212]] to ptr97; IR-NEXT: [[I14:%.*]] = load float, ptr [[I13]], align 498; IR-NEXT: [[I15:%.*]] = fadd float [[I10]], [[I14]]99; IR-NEXT: [[I1618:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 132100; IR-NEXT: [[I17:%.*]] = addrspacecast ptr addrspace(3) [[I1618]] to ptr101; IR-NEXT: [[I18:%.*]] = load float, ptr [[I17]], align 4102; IR-NEXT: [[I19:%.*]] = fadd float [[I15]], [[I18]]103; IR-NEXT: store float [[I19]], ptr [[OUTPUT]], align 4104; IR-NEXT: ret void105;106.preheader:107 %i = sext i32 %y to i64108 %i1 = sext i32 %x to i64109 %i2 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i110 %i3 = addrspacecast ptr addrspace(3) %i2 to ptr111 %i4 = load float, ptr %i3, align 4112 %i5 = fadd float %i4, 0.000000e+00113 %i6 = add i64 %i, 1114 %i7 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i6115 %i8 = addrspacecast ptr addrspace(3) %i7 to ptr116 %i9 = load float, ptr %i8, align 4117 %i10 = fadd float %i5, %i9118 %i11 = add i64 %i1, 1119 %i12 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i11, i64 %i120 %i13 = addrspacecast ptr addrspace(3) %i12 to ptr121 %i14 = load float, ptr %i13, align 4122 %i15 = fadd float %i10, %i14123 %i16 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i11, i64 %i6124 %i17 = addrspacecast ptr addrspace(3) %i16 to ptr125 %i18 = load float, ptr %i17, align 4126 %i19 = fadd float %i15, %i18127 store float %i19, ptr %output, align 4128 ret void129}130 131; This function loads132; array[zext(x)][zext(y)]133; array[zext(x)][zext(y +nuw 1)]134; array[zext(x +nuw 1)][zext(y)]135; array[zext(x +nuw 1)][zext(y +nuw 1)].136;137; This function is similar to @sum_of_array, but it138; 1) extends array indices using zext instead of sext;139; 2) annotates the addition with "nuw"; otherwise, zext(x + 1) => zext(x) + 1140; may be invalid.141 142define void @sum_of_array3(i32 %x, i32 %y, ptr nocapture %output) {143; IR-LABEL: define void @sum_of_array3144; IR-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], ptr captures(none) [[OUTPUT:%.*]]) #[[ATTR0]] {145; IR-NEXT: .preheader:146; IR-NEXT: [[I:%.*]] = zext i32 [[Y]] to i64147; IR-NEXT: [[I1:%.*]] = zext i32 [[X]] to i64148; IR-NEXT: [[I2:%.*]] = getelementptr [32 x [32 x float]], ptr addrspace(3) @array, i32 0, i32 [[X]], i32 [[Y]]149; IR-NEXT: [[I3:%.*]] = addrspacecast ptr addrspace(3) [[I2]] to ptr150; IR-NEXT: [[I4:%.*]] = load float, ptr [[I3]], align 4151; IR-NEXT: [[I5:%.*]] = fadd float [[I4]], 0.000000e+00152; IR-NEXT: [[I87:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 4153; IR-NEXT: [[I9:%.*]] = addrspacecast ptr addrspace(3) [[I87]] to ptr154; IR-NEXT: [[I10:%.*]] = load float, ptr [[I9]], align 4155; IR-NEXT: [[I11:%.*]] = fadd float [[I5]], [[I10]]156; IR-NEXT: [[I1412:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 128157; IR-NEXT: [[I15:%.*]] = addrspacecast ptr addrspace(3) [[I1412]] to ptr158; IR-NEXT: [[I16:%.*]] = load float, ptr [[I15]], align 4159; IR-NEXT: [[I17:%.*]] = fadd float [[I11]], [[I16]]160; IR-NEXT: [[I1818:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 132161; IR-NEXT: [[I19:%.*]] = addrspacecast ptr addrspace(3) [[I1818]] to ptr162; IR-NEXT: [[I20:%.*]] = load float, ptr [[I19]], align 4163; IR-NEXT: [[I21:%.*]] = fadd float [[I17]], [[I20]]164; IR-NEXT: store float [[I21]], ptr [[OUTPUT]], align 4165; IR-NEXT: ret void166;167.preheader:168 %i = zext i32 %y to i64169 %i1 = zext i32 %x to i64170 %i2 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i171 %i3 = addrspacecast ptr addrspace(3) %i2 to ptr172 %i4 = load float, ptr %i3, align 4173 %i5 = fadd float %i4, 0.000000e+00174 %i6 = add nuw i32 %y, 1175 %i7 = zext i32 %i6 to i64176 %i8 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i7177 %i9 = addrspacecast ptr addrspace(3) %i8 to ptr178 %i10 = load float, ptr %i9, align 4179 %i11 = fadd float %i5, %i10180 %i12 = add nuw i32 %x, 1181 %i13 = zext i32 %i12 to i64182 %i14 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i13, i64 %i183 %i15 = addrspacecast ptr addrspace(3) %i14 to ptr184 %i16 = load float, ptr %i15, align 4185 %i17 = fadd float %i11, %i16186 %i18 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i13, i64 %i7187 %i19 = addrspacecast ptr addrspace(3) %i18 to ptr188 %i20 = load float, ptr %i19, align 4189 %i21 = fadd float %i17, %i20190 store float %i21, ptr %output, align 4191 ret void192}193 194; This function loads195; array[zext(x)][zext(y)]196; array[zext(x)][zext(y)]197; array[zext(x) + 1][zext(y) + 1]198; array[zext(x) + 1][zext(y) + 1].199;200; We expect the generated code to reuse the computation of201; &array[zext(x)][zext(y)]. See the expected IR and PTX for details.202define void @sum_of_array4(i32 %x, i32 %y, ptr nocapture %output) {203; IR-LABEL: define void @sum_of_array4204; IR-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], ptr captures(none) [[OUTPUT:%.*]]) #[[ATTR0]] {205; IR-NEXT: .preheader:206; IR-NEXT: [[I:%.*]] = zext i32 [[Y]] to i64207; IR-NEXT: [[I1:%.*]] = zext i32 [[X]] to i64208; IR-NEXT: [[I2:%.*]] = getelementptr [32 x [32 x float]], ptr addrspace(3) @array, i32 0, i32 [[X]], i32 [[Y]]209; IR-NEXT: [[I3:%.*]] = addrspacecast ptr addrspace(3) [[I2]] to ptr210; IR-NEXT: [[I4:%.*]] = load float, ptr [[I3]], align 4211; IR-NEXT: [[I5:%.*]] = fadd float [[I4]], 0.000000e+00212; IR-NEXT: [[I77:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 4213; IR-NEXT: [[I8:%.*]] = addrspacecast ptr addrspace(3) [[I77]] to ptr214; IR-NEXT: [[I9:%.*]] = load float, ptr [[I8]], align 4215; IR-NEXT: [[I10:%.*]] = fadd float [[I5]], [[I9]]216; IR-NEXT: [[I1212:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 128217; IR-NEXT: [[I13:%.*]] = addrspacecast ptr addrspace(3) [[I1212]] to ptr218; IR-NEXT: [[I14:%.*]] = load float, ptr [[I13]], align 4219; IR-NEXT: [[I15:%.*]] = fadd float [[I10]], [[I14]]220; IR-NEXT: [[I1618:%.*]] = getelementptr i8, ptr addrspace(3) [[I2]], i32 132221; IR-NEXT: [[I17:%.*]] = addrspacecast ptr addrspace(3) [[I1618]] to ptr222; IR-NEXT: [[I18:%.*]] = load float, ptr [[I17]], align 4223; IR-NEXT: [[I19:%.*]] = fadd float [[I15]], [[I18]]224; IR-NEXT: store float [[I19]], ptr [[OUTPUT]], align 4225; IR-NEXT: ret void226;227.preheader:228 %i = zext i32 %y to i64229 %i1 = zext i32 %x to i64230 %i2 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i231 %i3 = addrspacecast ptr addrspace(3) %i2 to ptr232 %i4 = load float, ptr %i3, align 4233 %i5 = fadd float %i4, 0.000000e+00234 %i6 = add i64 %i, 1235 %i7 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i1, i64 %i6236 %i8 = addrspacecast ptr addrspace(3) %i7 to ptr237 %i9 = load float, ptr %i8, align 4238 %i10 = fadd float %i5, %i9239 %i11 = add i64 %i1, 1240 %i12 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i11, i64 %i241 %i13 = addrspacecast ptr addrspace(3) %i12 to ptr242 %i14 = load float, ptr %i13, align 4243 %i15 = fadd float %i10, %i14244 %i16 = getelementptr inbounds [32 x [32 x float]], ptr addrspace(3) @array, i64 0, i64 %i11, i64 %i6245 %i17 = addrspacecast ptr addrspace(3) %i16 to ptr246 %i18 = load float, ptr %i17, align 4247 %i19 = fadd float %i15, %i18248 store float %i19, ptr %output, align 4249 ret void250}251 252; The source code is:253; p0 = &input[sext(x + y)];254; p1 = &input[sext(x + (y + 5))];255;256; Without reuniting extensions, SeparateConstOffsetFromGEP would emit257; p0 = &input[sext(x + y)];258; t1 = &input[sext(x) + sext(y)];259; p1 = &t1[5];260;261; With reuniting extensions, it merges p0 and t1 and thus emits262; p0 = &input[sext(x + y)];263; p1 = &p0[5];264define void @reunion(i32 %x, i32 %y, ptr %input) {265; IR-LABEL: define void @reunion266; IR-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], ptr [[INPUT:%.*]]) #[[ATTR0]] {267; IR-NEXT: entry:268; IR-NEXT: [[XY:%.*]] = add nsw i32 [[X]], [[Y]]269; IR-NEXT: [[I:%.*]] = sext i32 [[XY]] to i64270; IR-NEXT: [[P0:%.*]] = getelementptr float, ptr [[INPUT]], i64 [[I]]271; IR-NEXT: [[V0:%.*]] = load float, ptr [[P0]], align 4272; IR-NEXT: call void @use(float [[V0]])273; IR-NEXT: [[P13:%.*]] = getelementptr i8, ptr [[P0]], i64 20274; IR-NEXT: [[V1:%.*]] = load float, ptr [[P13]], align 4275; IR-NEXT: call void @use(float [[V1]])276; IR-NEXT: ret void277;278entry:279 %xy = add nsw i32 %x, %y280 %i = sext i32 %xy to i64281 %p0 = getelementptr inbounds float, ptr %input, i64 %i282 %v0 = load float, ptr %p0, align 4283 call void @use(float %v0)284 %y5 = add nsw i32 %y, 5285 %xy5 = add nsw i32 %x, %y5286 %i1 = sext i32 %xy5 to i64287 %p1 = getelementptr inbounds float, ptr %input, i64 %i1288 %v1 = load float, ptr %p1, align 4289 call void @use(float %v1)290 ret void291}292 293declare void @use(float)294