From 1fb6c508965d39008f91182b44623a402b24d945 Mon Sep 17 00:00:00 2001 From: TB Schardl Date: Mon, 13 Jun 2022 11:29:55 +0000 Subject: [PATCH] [CGCilk] Fix code generation of unreachable _Cilk_sync statements. Fixes issue #93. --- clang/lib/CodeGen/CGCilk.cpp | 12 +++++++----- clang/test/Cilk/unreachable-sync.cpp | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 clang/test/Cilk/unreachable-sync.cpp diff --git a/clang/lib/CodeGen/CGCilk.cpp b/clang/lib/CodeGen/CGCilk.cpp index fd3df8efcc8f0d..6bc5411cbfbca1 100644 --- a/clang/lib/CodeGen/CGCilk.cpp +++ b/clang/lib/CodeGen/CGCilk.cpp @@ -431,11 +431,13 @@ llvm::Instruction *CodeGenFunction::EmitSyncRegionStart() { void CodeGenFunction::EmitCilkSyncStmt(const CilkSyncStmt &S) { llvm::BasicBlock *ContinueBlock = createBasicBlock("sync.continue"); - // If this code is reachable then emit a stop point (if generating - // debug info). We have to do this ourselves because we are on the - // "simple" statement path. - if (HaveInsertPoint()) - EmitStopPoint(&S); + // Check if we are generating unreachable code. + if (!HaveInsertPoint()) + // We don't need to generate actual code. + return; + + // Generate a stoppoint if we are emitting debug info. + EmitStopPoint(&S); EnsureSyncRegion(); diff --git a/clang/test/Cilk/unreachable-sync.cpp b/clang/test/Cilk/unreachable-sync.cpp new file mode 100644 index 00000000000000..7aa96518db65da --- /dev/null +++ b/clang/test/Cilk/unreachable-sync.cpp @@ -0,0 +1,18 @@ +// Check that a sync is not inserted when clang recognizes that it is not reachable. +// +// RUN: %clang_cc1 -fopencilk -ftapir=none -triple x86_64-unknown-linux-gnu -std=c++11 -emit-llvm %s -o - | FileCheck %s +int create() { + return 1; + _Cilk_sync; +} + +// CHECK: define dso_local {{.*}}i32 @_Z6createv() +// CHECK: { +// CHECK: ret i32 1 +// CHECK-NOT: sync +// CHECK: } + +int main([[maybe_unused]] int argc, char *argv[]) { + int e = create(); + return 0; +}