252 lines · cpp
1// Check code generation2// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -emit-llvm %s -o - | FileCheck %s --check-prefix=IR3 4// Check same results after serialization round-trip5// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -emit-pch -o %t %s6// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -include-pch %t -emit-llvm %s -o - | FileCheck %s --check-prefix=IR7// expected-no-diagnostics8 9#ifndef HEADER10#define HEADER11 12// placeholder for loop body code.13extern "C" void body(...) {}14 15 16// IR-LABEL: @func(17// IR-NEXT: [[ENTRY:.*]]:18// IR-NEXT: %[[START_ADDR:.+]] = alloca i32, align 419// IR-NEXT: %[[END_ADDR:.+]] = alloca i32, align 420// IR-NEXT: %[[STEP_ADDR:.+]] = alloca i32, align 421// IR-NEXT: %[[DOTOMP_IV:.+]] = alloca i64, align 822// IR-NEXT: %[[TMP:.+]] = alloca i32, align 423// IR-NEXT: %[[TMP1:.+]] = alloca i32, align 424// IR-NEXT: %[[DOTCAPTURE_EXPR_:.+]] = alloca i32, align 425// IR-NEXT: %[[DOTCAPTURE_EXPR_2:.+]] = alloca i32, align 426// IR-NEXT: %[[DOTCAPTURE_EXPR_3:.+]] = alloca i32, align 427// IR-NEXT: %[[J:.+]] = alloca i32, align 428// IR-NEXT: %[[DOTCAPTURE_EXPR_4:.+]] = alloca i32, align 429// IR-NEXT: %[[DOTCAPTURE_EXPR_5:.+]] = alloca i32, align 430// IR-NEXT: %[[DOTCAPTURE_EXPR_6:.+]] = alloca i32, align 431// IR-NEXT: %[[DOTCAPTURE_EXPR_7:.+]] = alloca i32, align 432// IR-NEXT: %[[DOTCAPTURE_EXPR_10:.+]] = alloca i32, align 433// IR-NEXT: %[[DOTCAPTURE_EXPR_12:.+]] = alloca i64, align 834// IR-NEXT: %[[I:.+]] = alloca i32, align 435// IR-NEXT: %[[DOTUNROLLED_IV_J:.+]] = alloca i32, align 436// IR-NEXT: %[[DOTOMP_LB:.+]] = alloca i64, align 837// IR-NEXT: %[[DOTOMP_UB:.+]] = alloca i64, align 838// IR-NEXT: %[[DOTOMP_STRIDE:.+]] = alloca i64, align 839// IR-NEXT: %[[DOTOMP_IS_LAST:.+]] = alloca i32, align 440// IR-NEXT: %[[I22:.+]] = alloca i32, align 441// IR-NEXT: %[[DOTUNROLLED_IV_J23:.+]] = alloca i32, align 442// IR-NEXT: %[[DOTUNROLL_INNER_IV_J:.+]] = alloca i32, align 443// IR-NEXT: %[[TMP0:.+]] = call i32 @__kmpc_global_thread_num(ptr @2)44// IR-NEXT: store i32 %[[START:.+]], ptr %[[START_ADDR]], align 445// IR-NEXT: store i32 %[[END:.+]], ptr %[[END_ADDR]], align 446// IR-NEXT: store i32 %[[STEP:.+]], ptr %[[STEP_ADDR]], align 447// IR-NEXT: %[[TMP1_1:.+]] = load i32, ptr %[[START_ADDR]], align 448// IR-NEXT: store i32 %[[TMP1_1]], ptr %[[DOTCAPTURE_EXPR_]], align 449// IR-NEXT: %[[TMP2:.+]] = load i32, ptr %[[END_ADDR]], align 450// IR-NEXT: store i32 %[[TMP2]], ptr %[[DOTCAPTURE_EXPR_2]], align 451// IR-NEXT: %[[TMP3:.+]] = load i32, ptr %[[STEP_ADDR]], align 452// IR-NEXT: store i32 %[[TMP3]], ptr %[[DOTCAPTURE_EXPR_3]], align 453// IR-NEXT: %[[TMP4:.+]] = load i32, ptr %[[START_ADDR]], align 454// IR-NEXT: store i32 %[[TMP4]], ptr %[[J]], align 455// IR-NEXT: %[[TMP5:.+]] = load i32, ptr %[[START_ADDR]], align 456// IR-NEXT: store i32 %[[TMP5]], ptr %[[DOTCAPTURE_EXPR_4]], align 457// IR-NEXT: %[[TMP6:.+]] = load i32, ptr %[[END_ADDR]], align 458// IR-NEXT: store i32 %[[TMP6]], ptr %[[DOTCAPTURE_EXPR_5]], align 459// IR-NEXT: %[[TMP7:.+]] = load i32, ptr %[[STEP_ADDR]], align 460// IR-NEXT: store i32 %[[TMP7]], ptr %[[DOTCAPTURE_EXPR_6]], align 461// IR-NEXT: %[[TMP8:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_5]], align 462// IR-NEXT: %[[TMP9:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_4]], align 463// IR-NEXT: %[[SUB:.+]] = sub i32 %[[TMP8]], %[[TMP9]]64// IR-NEXT: %[[SUB8:.+]] = sub i32 %[[SUB]], 165// IR-NEXT: %[[TMP10:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_6]], align 466// IR-NEXT: %[[ADD:.+]] = add i32 %[[SUB8]], %[[TMP10]]67// IR-NEXT: %[[TMP11:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_6]], align 468// IR-NEXT: %[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP11]]69// IR-NEXT: %[[SUB9:.+]] = sub i32 %[[DIV]], 170// IR-NEXT: store i32 %[[SUB9]], ptr %[[DOTCAPTURE_EXPR_7]], align 471// IR-NEXT: %[[TMP12:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_7]], align 472// IR-NEXT: %[[ADD11:.+]] = add i32 %[[TMP12]], 173// IR-NEXT: store i32 %[[ADD11]], ptr %[[DOTCAPTURE_EXPR_10]], align 474// IR-NEXT: %[[TMP13:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_2]], align 475// IR-NEXT: %[[TMP14:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 476// IR-NEXT: %[[SUB13:.+]] = sub i32 %[[TMP13]], %[[TMP14]]77// IR-NEXT: %[[SUB14:.+]] = sub i32 %[[SUB13]], 178// IR-NEXT: %[[TMP15:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_3]], align 479// IR-NEXT: %[[ADD15:.+]] = add i32 %[[SUB14]], %[[TMP15]]80// IR-NEXT: %[[TMP16:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_3]], align 481// IR-NEXT: %[[DIV16:.+]] = udiv i32 %[[ADD15]], %[[TMP16]]82// IR-NEXT: %[[CONV:.+]] = zext i32 %[[DIV16]] to i6483// IR-NEXT: %[[TMP17:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_10]], align 484// IR-NEXT: %[[SUB17:.+]] = sub i32 %[[TMP17]], -185// IR-NEXT: %[[DIV18:.+]] = udiv i32 %[[SUB17]], 286// IR-NEXT: %[[CONV19:.+]] = zext i32 %[[DIV18]] to i6487// IR-NEXT: %[[MUL:.+]] = mul nsw i64 %[[CONV]], %[[CONV19]]88// IR-NEXT: %[[SUB20:.+]] = sub nsw i64 %[[MUL]], 189// IR-NEXT: store i64 %[[SUB20]], ptr %[[DOTCAPTURE_EXPR_12]], align 890// IR-NEXT: %[[TMP18:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 491// IR-NEXT: store i32 %[[TMP18]], ptr %[[I]], align 492// IR-NEXT: store i32 0, ptr %[[DOTUNROLLED_IV_J]], align 493// IR-NEXT: %[[TMP19:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 494// IR-NEXT: %[[TMP20:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_2]], align 495// IR-NEXT: %[[CMP:.+]] = icmp slt i32 %[[TMP19]], %[[TMP20]]96// IR-NEXT: br i1 %[[CMP]], label %[[LAND_LHS_TRUE:.+]], label %[[OMP_PRECOND_END:.+]]97// IR-EMPTY:98// IR-NEXT: [[LAND_LHS_TRUE]]:99// IR-NEXT: %[[TMP21:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_10]], align 4100// IR-NEXT: %[[CMP21:.+]] = icmp ult i32 0, %[[TMP21]]101// IR-NEXT: br i1 %[[CMP21]], label %[[OMP_PRECOND_THEN:.+]], label %[[OMP_PRECOND_END]]102// IR-EMPTY:103// IR-NEXT: [[OMP_PRECOND_THEN]]:104// IR-NEXT: store i64 0, ptr %[[DOTOMP_LB]], align 8105// IR-NEXT: %[[TMP22:.+]] = load i64, ptr %[[DOTCAPTURE_EXPR_12]], align 8106// IR-NEXT: store i64 %[[TMP22]], ptr %[[DOTOMP_UB]], align 8107// IR-NEXT: store i64 1, ptr %[[DOTOMP_STRIDE]], align 8108// IR-NEXT: store i32 0, ptr %[[DOTOMP_IS_LAST]], align 4109// IR-NEXT: call void @__kmpc_for_static_init_8(ptr @1, i32 %[[TMP0]], i32 34, ptr %[[DOTOMP_IS_LAST]], ptr %[[DOTOMP_LB]], ptr %[[DOTOMP_UB]], ptr %[[DOTOMP_STRIDE]], i64 1, i64 1)110// IR-NEXT: %[[TMP23:.+]] = load i64, ptr %[[DOTOMP_UB]], align 8111// IR-NEXT: %[[TMP24:.+]] = load i64, ptr %[[DOTCAPTURE_EXPR_12]], align 8112// IR-NEXT: %[[CMP24:.+]] = icmp sgt i64 %[[TMP23]], %[[TMP24]]113// IR-NEXT: br i1 %[[CMP24]], label %[[COND_TRUE:.+]], label %[[COND_FALSE:.+]]114// IR-EMPTY:115// IR-NEXT: [[COND_TRUE]]:116// IR-NEXT: %[[TMP25:.+]] = load i64, ptr %[[DOTCAPTURE_EXPR_12]], align 8117// IR-NEXT: br label %[[COND_END:.+]]118// IR-EMPTY:119// IR-NEXT: [[COND_FALSE]]:120// IR-NEXT: %[[TMP26:.+]] = load i64, ptr %[[DOTOMP_UB]], align 8121// IR-NEXT: br label %[[COND_END]]122// IR-EMPTY:123// IR-NEXT: [[COND_END]]:124// IR-NEXT: %[[COND:.+]] = phi i64 [ %[[TMP25]], %[[COND_TRUE]] ], [ %[[TMP26]], %[[COND_FALSE]] ]125// IR-NEXT: store i64 %[[COND]], ptr %[[DOTOMP_UB]], align 8126// IR-NEXT: %[[TMP27:.+]] = load i64, ptr %[[DOTOMP_LB]], align 8127// IR-NEXT: store i64 %[[TMP27]], ptr %[[DOTOMP_IV]], align 8128// IR-NEXT: br label %[[OMP_INNER_FOR_COND:.+]]129// IR-EMPTY:130// IR-NEXT: [[OMP_INNER_FOR_COND]]:131// IR-NEXT: %[[TMP28:.+]] = load i64, ptr %[[DOTOMP_IV]], align 8132// IR-NEXT: %[[TMP29:.+]] = load i64, ptr %[[DOTOMP_UB]], align 8133// IR-NEXT: %[[CMP25:.+]] = icmp sle i64 %[[TMP28]], %[[TMP29]]134// IR-NEXT: br i1 %[[CMP25]], label %[[OMP_INNER_FOR_BODY:.+]], label %[[OMP_INNER_FOR_END:.+]]135// IR-EMPTY:136// IR-NEXT: [[OMP_INNER_FOR_BODY]]:137// IR-NEXT: %[[TMP30:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 4138// IR-NEXT: %[[CONV26:.+]] = sext i32 %[[TMP30]] to i64139// IR-NEXT: %[[TMP31:.+]] = load i64, ptr %[[DOTOMP_IV]], align 8140// IR-NEXT: %[[TMP32:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_10]], align 4141// IR-NEXT: %[[SUB27:.+]] = sub i32 %[[TMP32]], -1142// IR-NEXT: %[[DIV28:.+]] = udiv i32 %[[SUB27]], 2143// IR-NEXT: %[[MUL29:.+]] = mul i32 1, %[[DIV28]]144// IR-NEXT: %[[CONV30:.+]] = zext i32 %[[MUL29]] to i64145// IR-NEXT: %[[DIV31:.+]] = sdiv i64 %[[TMP31]], %[[CONV30]]146// IR-NEXT: %[[TMP33:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_3]], align 4147// IR-NEXT: %[[CONV32:.+]] = sext i32 %[[TMP33]] to i64148// IR-NEXT: %[[MUL33:.+]] = mul nsw i64 %[[DIV31]], %[[CONV32]]149// IR-NEXT: %[[ADD34:.+]] = add nsw i64 %[[CONV26]], %[[MUL33]]150// IR-NEXT: %[[CONV35:.+]] = trunc i64 %[[ADD34]] to i32151// IR-NEXT: store i32 %[[CONV35]], ptr %[[I22]], align 4152// IR-NEXT: %[[TMP34:.+]] = load i64, ptr %[[DOTOMP_IV]], align 8153// IR-NEXT: %[[TMP35:.+]] = load i64, ptr %[[DOTOMP_IV]], align 8154// IR-NEXT: %[[TMP36:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_10]], align 4155// IR-NEXT: %[[SUB36:.+]] = sub i32 %[[TMP36]], -1156// IR-NEXT: %[[DIV37:.+]] = udiv i32 %[[SUB36]], 2157// IR-NEXT: %[[MUL38:.+]] = mul i32 1, %[[DIV37]]158// IR-NEXT: %[[CONV39:.+]] = zext i32 %[[MUL38]] to i64159// IR-NEXT: %[[DIV40:.+]] = sdiv i64 %[[TMP35]], %[[CONV39]]160// IR-NEXT: %[[TMP37:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_10]], align 4161// IR-NEXT: %[[SUB41:.+]] = sub i32 %[[TMP37]], -1162// IR-NEXT: %[[DIV42:.+]] = udiv i32 %[[SUB41]], 2163// IR-NEXT: %[[MUL43:.+]] = mul i32 1, %[[DIV42]]164// IR-NEXT: %[[CONV44:.+]] = zext i32 %[[MUL43]] to i64165// IR-NEXT: %[[MUL45:.+]] = mul nsw i64 %[[DIV40]], %[[CONV44]]166// IR-NEXT: %[[SUB46:.+]] = sub nsw i64 %[[TMP34]], %[[MUL45]]167// IR-NEXT: %[[MUL47:.+]] = mul nsw i64 %[[SUB46]], 2168// IR-NEXT: %[[ADD48:.+]] = add nsw i64 0, %[[MUL47]]169// IR-NEXT: %[[CONV49:.+]] = trunc i64 %[[ADD48]] to i32170// IR-NEXT: store i32 %[[CONV49]], ptr %[[DOTUNROLLED_IV_J23]], align 4171// IR-NEXT: %[[TMP38:.+]] = load i32, ptr %[[DOTUNROLLED_IV_J23]], align 4172// IR-NEXT: store i32 %[[TMP38]], ptr %[[DOTUNROLL_INNER_IV_J]], align 4173// IR-NEXT: br label %[[FOR_COND:.+]]174// IR-EMPTY:175// IR-NEXT: [[FOR_COND]]:176// IR-NEXT: %[[TMP39:.+]] = load i32, ptr %[[DOTUNROLL_INNER_IV_J]], align 4177// IR-NEXT: %[[TMP40:.+]] = load i32, ptr %[[DOTUNROLLED_IV_J23]], align 4178// IR-NEXT: %[[ADD50:.+]] = add i32 %[[TMP40]], 2179// IR-NEXT: %[[CMP51:.+]] = icmp ult i32 %[[TMP39]], %[[ADD50]]180// IR-NEXT: br i1 %[[CMP51]], label %[[LAND_RHS:.+]], label %[[LAND_END:.+]]181// IR-EMPTY:182// IR-NEXT: [[LAND_RHS]]:183// IR-NEXT: %[[TMP41:.+]] = load i32, ptr %[[DOTUNROLL_INNER_IV_J]], align 4184// IR-NEXT: %[[TMP42:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_7]], align 4185// IR-NEXT: %[[ADD52:.+]] = add i32 %[[TMP42]], 1186// IR-NEXT: %[[CMP53:.+]] = icmp ult i32 %[[TMP41]], %[[ADD52]]187// IR-NEXT: br label %[[LAND_END]]188// IR-EMPTY:189// IR-NEXT: [[LAND_END]]:190// IR-NEXT: %[[TMP43:.+]] = phi i1 [ false, %[[FOR_COND]] ], [ %[[CMP53]], %[[LAND_RHS]] ]191// IR-NEXT: br i1 %[[TMP43]], label %[[FOR_BODY:.+]], label %[[FOR_END:.+]]192// IR-EMPTY:193// IR-NEXT: [[FOR_BODY]]:194// IR-NEXT: %[[TMP44:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_4]], align 4195// IR-NEXT: %[[TMP45:.+]] = load i32, ptr %[[DOTUNROLL_INNER_IV_J]], align 4196// IR-NEXT: %[[TMP46:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_6]], align 4197// IR-NEXT: %[[MUL54:.+]] = mul i32 %[[TMP45]], %[[TMP46]]198// IR-NEXT: %[[ADD55:.+]] = add i32 %[[TMP44]], %[[MUL54]]199// IR-NEXT: store i32 %[[ADD55]], ptr %[[J]], align 4200// IR-NEXT: %[[TMP47:.+]] = load i32, ptr %[[START_ADDR]], align 4201// IR-NEXT: %[[TMP48:.+]] = load i32, ptr %[[END_ADDR]], align 4202// IR-NEXT: %[[TMP49:.+]] = load i32, ptr %[[STEP_ADDR]], align 4203// IR-NEXT: %[[TMP50:.+]] = load i32, ptr %[[I22]], align 4204// IR-NEXT: %[[TMP51:.+]] = load i32, ptr %[[J]], align 4205// IR-NEXT: call void (...) @body(i32 noundef %[[TMP47]], i32 noundef %[[TMP48]], i32 noundef %[[TMP49]], i32 noundef %[[TMP50]], i32 noundef %[[TMP51]])206// IR-NEXT: br label %[[FOR_INC:.+]]207// IR-EMPTY:208// IR-NEXT: [[FOR_INC]]:209// IR-NEXT: %[[TMP52:.+]] = load i32, ptr %[[DOTUNROLL_INNER_IV_J]], align 4210// IR-NEXT: %[[INC:.+]] = add i32 %[[TMP52]], 1211// IR-NEXT: store i32 %[[INC]], ptr %[[DOTUNROLL_INNER_IV_J]], align 4212// IR-NEXT: br label %[[FOR_COND]], !llvm.loop ![[LOOP2:[0-9]+]]213// IR-EMPTY:214// IR-NEXT: [[FOR_END]]:215// IR-NEXT: br label %[[OMP_BODY_CONTINUE:.+]]216// IR-EMPTY:217// IR-NEXT: [[OMP_BODY_CONTINUE]]:218// IR-NEXT: br label %[[OMP_INNER_FOR_INC:.+]]219// IR-EMPTY:220// IR-NEXT: [[OMP_INNER_FOR_INC]]:221// IR-NEXT: %[[TMP53:.+]] = load i64, ptr %[[DOTOMP_IV]], align 8222// IR-NEXT: %[[ADD56:.+]] = add nsw i64 %[[TMP53]], 1223// IR-NEXT: store i64 %[[ADD56]], ptr %[[DOTOMP_IV]], align 8224// IR-NEXT: br label %[[OMP_INNER_FOR_COND]]225// IR-EMPTY:226// IR-NEXT: [[OMP_INNER_FOR_END]]:227// IR-NEXT: br label %[[OMP_LOOP_EXIT:.+]]228// IR-EMPTY:229// IR-NEXT: [[OMP_LOOP_EXIT]]:230// IR-NEXT: call void @__kmpc_for_static_fini(ptr @1, i32 %[[TMP0]])231// IR-NEXT: br label %[[OMP_PRECOND_END]]232// IR-EMPTY:233// IR-NEXT: [[OMP_PRECOND_END]]:234// IR-NEXT: call void @__kmpc_barrier(ptr @3, i32 %[[TMP0]])235// IR-NEXT: ret void236// IR-NEXT: }237extern "C" void func(int start, int end, int step) {238 #pragma omp for collapse(2)239 for (int i = start; i < end; i+=step) {240 #pragma omp unroll partial241 for (int j = start; j < end; j+=step)242 body(start, end, step, i, j);243 }244}245 246#endif /* HEADER */247 248 249// IR: ![[LOOP2]] = distinct !{![[LOOP2]], ![[LOOPPROP3:[0-9]+]], ![[LOOPPROP4:[0-9]+]]}250// IR: ![[LOOPPROP3]] = !{!"llvm.loop.mustprogress"}251// IR: ![[LOOPPROP4]] = !{!"llvm.loop.unroll.count", i32 2}252