[LLVMbugs] [Bug 19821] New: std::atomic<Foo>::load() hits an assert after EmitAtomicExpr()

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed May 21 16:09:55 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=19821

            Bug ID: 19821
           Summary: std::atomic<Foo>::load() hits an assert after
                    EmitAtomicExpr()
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: tomislav.novak at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 12556
  --> http://llvm.org/bugs/attachment.cgi?id=12556&action=edit
Preprocessed source

For non-builtin types, std::atomic<T> is implemented via 'void
__atomic_load(type *ptr, type *ret, int memmodel)' in libstdc++ (I've tested
with 4.8.2). If alignof(T) != sizeof(T), clang will emit a library call for
that (as opposed to doing it natively). The following code hits an assert:

#include <atomic>
#include <cstdio>

struct Foo { int x, y; };

int main() {
  Foo conventional{1, 2};
  std::atomic<Foo> atomic;
  atomic.store(conventional);
  std::printf("(%d, %d)\n", atomic.load().x, atomic.load().y);
  return 0;
}

Full stack trace is below. The issue seems to be
CodeGenFunction::EmitAtomicExpr() not handling the case in which the atomic
expression is void (__atomic_load in this case), while the library call returns
a value (__atomic_load_8).

The following diff fixed it for me:

Index: lib/CodeGen/CGAtomic.cpp
===================================================================
--- lib/CodeGen/CGAtomic.cpp    (revision 209250)
+++ lib/CodeGen/CGAtomic.cpp    (working copy)
@@ -692,11 +692,15 @@
             FunctionType::ExtInfo(), RequiredArgs::All);
     llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
     llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
-    RValue Res = EmitCall(FuncInfo, Func, ReturnValueSlot(), Args);
+    ReturnValueSlot Slot;
+    if (!RetTy->isVoidType() && E->getType()->isVoidType())
+      Slot = ReturnValueSlot(Dest, false);
+    RValue Res = EmitCall(FuncInfo, Func, Slot, Args);
+
+    if (E->getType()->isVoidType())
+      return RValue::get(0);
     if (!RetTy->isVoidType())
       return Res;
-    if (E->getType()->isVoidType())
-      return RValue::get(0);
     return convertTempToRValue(Dest, E->getType(), E->getExprLoc());
   }




-*-

$ clang++ atomic_test.cpp -std=c++0x
clang: /data/users/tnovak/src/llvm/tools/clang/lib/CodeGen/CGValue.h:54:
llvm::Value* clang::CodeGen::RValue::getScalarVal() const: Assertion
`isScalar() && "Not a scalar!"' failed.
0  clang           0x0000000003a32929 llvm::sys::PrintStackTrace(_IO_FILE*) +
38
1  clang           0x0000000003a32ba6
2  clang           0x0000000003a32555
3  libpthread.so.0 0x00007ffc782cb500
4  libc.so.6       0x00007ffc773ac8a5 gsignal + 53
5  libc.so.6       0x00007ffc773ae085 abort + 373
6  libc.so.6       0x00007ffc773a5a1e
7  libc.so.6       0x00007ffc773a5ae0 __assert_perror_fail + 0
8  clang           0x00000000012b715e
9  clang           0x00000000014415bf
10 clang           0x00000000014427f1
11 clang           0x0000000001431ad9
12 clang           0x0000000001441661
clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) + 159
13 clang           0x00000000013ffbeb
clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*,
clang::CodeGen::AggValueSlot, bool) + 91
14 clang           0x00000000013ffb74
clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) + 62
15 clang           0x00000000012b8386
clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*) + 428
16 clang           0x00000000012b8a20
clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt
const&, bool, clang::CodeGen::AggValueSlot) + 118
17 clang           0x00000000012d84a6
clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::CodeGen::FunctionArgList&,
clang::Stmt const*) + 130
18 clang           0x00000000012d8dce
clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl,
llvm::Function*, clang::CodeGen::CGFunctionInfo const&) + 1598
19 clang           0x00000000012ec8a1
clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl,
llvm::GlobalValue*) + 973
20 clang           0x00000000012e9ad3
clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl,
llvm::GlobalValue*) + 349
21 clang           0x00000000012e8673
clang::CodeGen::CodeGenModule::EmitDeferred() + 321
22 clang           0x00000000012e564b clang::CodeGen::CodeGenModule::Release()
+ 29
23 clang           0x00000000012689ed
24 clang           0x0000000001264b15
25 clang           0x00000000014c8f26 clang::ParseAST(clang::Sema&, bool, bool)
+ 783
26 clang           0x0000000000fc9ab2 clang::ASTFrontendAction::ExecuteAction()
+ 322
27 clang           0x0000000001266c18 clang::CodeGenAction::ExecuteAction() +
1224
28 clang           0x0000000000fc95e7 clang::FrontendAction::Execute() + 139
29 clang           0x0000000000f977cc
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 720
30 clang           0x0000000000f5f921
clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1099
31 clang           0x0000000000f4e1c3 cc1_main(char const**, char const**, char
const*, void*) + 641
32 clang           0x0000000000f5a661 main + 760
33 libc.so.6       0x00007ffc77398cdd __libc_start_main + 253
34 clang           0x0000000000f4c8c9
Stack dump:
0.      Program arguments: /data/users/tnovak/toolchain/bin/clang -cc1 -triple
x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name
atomic_test.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno
-masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array
-target-cpu x86-64 -target-linker-version 2.20.51.0.2 -resource-dir
/data/users/tnovak/toolchain/bin/../lib/clang/3.5.0 -internal-isystem
/home/tnovak/local/toolchain/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2
-internal-isystem
/home/tnovak/local/toolchain/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/x86_64-unknown-linux-gnu
-internal-isystem
/home/tnovak/local/toolchain/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/backward
-internal-isystem
/home/tnovak/local/toolchain/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/x86_64-unknown-linux-gnu/c++/4.8.2
-internal-isystem /usr/local/include -internal-isystem
/data/users/tnovak/toolchain/bin/../lib/clang/3.5.0/include
-internal-externc-isystem /include -internal-externc-isystem /usr/include
-std=c++0x -fdeprecated-macro -fdebug-compilation-dir
/home/tnovak/fbcode/experimental/lovro -ferror-limit 19 -fmessage-length 309
-mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions
-fdiagnostics-show-option -o /tmp/atomic_test-6e7f5c.o -x c++ atomic_test.cpp
1.      <eof> parser at end of file
2.      Per-file LLVM IR generation
3.     
/home/tnovak/local/toolchain/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../include/c++/4.8.2/atomic:206:7:
Generating code for declaration 'std::atomic<Foo>::load'
clang: error: unable to execute command: Aborted
clang: error: clang frontend command failed due to signal (use -v to see
invocation)
clang version 3.5.0 (trunk 209250)
Target: x86_64-unknown-linux-gnu
Thread model: posix

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140521/0f28865c/attachment.html>


More information about the llvm-bugs mailing list