Skip to content

Commit

Permalink
[CGCilk] Fix cilk_for-loop code generation when emission of loop
Browse files Browse the repository at this point in the history
variable may introduce new basic blocks.  Addresses issue llvm#77.
  • Loading branch information
neboat committed Jul 1, 2023
1 parent 3cbb463 commit 626d7fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ void CodeGenFunction::EmitCilkForStmt(const CilkForStmt &S,
// detach.
if (InvokeDest) {
CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
Builder.SetInsertPoint(DetachBlock);
Builder.SetInsertPoint(Detach);
// Create the new detach instruction.
llvm::DetachInst *NewDetach = Builder.CreateDetach(
ForBodyEntry, Continue.getBlock(), InvokeDest,
Expand Down
50 changes: 50 additions & 0 deletions clang/test/Cilk/cilkfor-detach-unwind-rewrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Check that detaches with unwind destinations are properly inserted
// during CodeGen.
//
// RUN: %clang_cc1 %s -fopencilk -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fcxx-exceptions -fexceptions -emit-llvm -fsanitize-recover=signed-integer-overflow,unsigned-integer-overflow -disable-llvm-passes -o - | FileCheck %s

unsigned __cilkrts_get_nworkers(void);

template <class T> class Reducer_sum {
public:
Reducer_sum(long num_workers);
~Reducer_sum();
void add(T new_value);
T get() const;
};

int main() {
Reducer_sum<long> total_red(__cilkrts_get_nworkers());
_Cilk_for(long i = 0; i < 100; i++) { total_red.add(5); }
return total_red.get();
}

// CHECK: define {{.*}}i32 @main()
// CHECK: br i1 %{{.*}}, label %[[PFOR_PH:.+]], label %[[PFOR_END:[a-z0-9._]+]]

// CHECK: [[PFOR_PH]]:
// CHECK: call void @__ubsan_handle_sub_overflow
// CHECK: call void @__ubsan_handle_sub_overflow
// CHECK: call void @__ubsan_handle_divrem_overflow
// CHECK: call void @__ubsan_handle_add_overflow

// Check contents of the detach block
// CHECK: load i64, i64* %[[INIT:.+]]
// CHECK: load i64, i64* %[[BEGIN:.+]]
// CHECK: call { i64, i1 } @llvm.smul.with.overflow.i64(
// CHECK: br i1 %{{.*}}, label %[[CONT5:.+]], label %[[HANDLE_MUL_OVERFLOW:[a-z0-9._]+]],

// CHECK: [[HANDLE_MUL_OVERFLOW]]:
// CHECK: call void @__ubsan_handle_mul_overflow

// CHECK: [[CONT5]]:
// CHECK: call { i64, i1 } @llvm.sadd.with.overflow.i64(
// CHECK: br i1 %{{.*}}, label %[[CONT7:.+]], label %[[HANDLE_ADD_OVERFLOW:[a-z0-9._]+]],

// CHECK: [[HANDLE_ADD_OVERFLOW]]:
// CHECK: call void @__ubsan_handle_add_overflow

// Check that the detach ends up after the loop-variable init expression.

// CHECK: [[CONT7]]:
// CHECK-NEXT: detach within %[[SYNCREG:.+]], label %[[PFOR_BODY_ENTRY:.+]], label %[[PFOR_INC:.+]] unwind label %[[LPAD9:.+]]

0 comments on commit 626d7fe

Please sign in to comment.