<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61053>61053</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[OpenMP] Failure to find local declaration for `use_device_ptr` clause
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jhuber6
</td>
</tr>
</table>
<pre>
The following program crashes from executing an `llvm_unreachable` instruction. This is caused by the captured variable not having an entry in the local declaration map. Here is a godbolt link and the following reproducer https://godbolt.org/z/x6b935zG5.
```
template <int T> class A {
double *ptr = nullptr;
public:
void foo() {
#pragma omp target data use_device_ptr(ptr)
{ double *capture = ptr; }
}
};
template class A<0>;
```
This is all that is required to reproduce this bug.
```
$ clang++ bug.cpp -fopenmp
DeclRefExpr for Decl not entered in LocalDeclMap?
UNREACHABLE executed at clang/lib/CodeGen/CGExpr.cpp:2876!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: clang++ bug.cpp -fopenmp -c
1. <eof> parser at end of file
2. Per-file LLVM IR generation
3. bug.cpp:5:8: Generating code for declaration 'A<0>::foo'
#0 0x00007f000f221ace llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/jhuber/Documents/llvm/clang/lib/libLLVMSupport.so.17git+0x221ace)
#1 0x00007f000f21f38b llvm::sys::RunSignalHandlers() (/home/jhuber/Documents/llvm/clang/lib/libLLVMSupport.so.17git+0x21f38b)
#2 0x00007f000f12d528 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007f000ea51f50 (/usr/lib/libc.so.6+0x38f50)
#4 0x00007f000eaa08ec (/usr/lib/libc.so.6+0x878ec)
#5 0x00007f000ea51ea8 raise (/usr/lib/libc.so.6+0x38ea8)
#6 0x00007f000ea3b53d abort (/usr/lib/libc.so.6+0x2253d)
#7 0x00007f000f146cfa (/home/jhuber/Documents/llvm/clang/lib/libLLVMSupport.so.17git+0x146cfa)
#8 0x00007f001390ce8d clang::CodeGen::CodeGenFunction::EmitDeclRefLValue(clang::DeclRefExpr const*) (/home/jhuber/Documents/llvm/clang/lib/libclangCodeGen.so.17git+0x50ce8d)
#9 0x00007f0013908098 clang::CodeGen::CodeGenFunction::EmitLValueHelper(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/home/jhuber/Documents/llvm/clang/lib/libclangCodeGen.so.17git+0x508098)
#10 0x00007f0013908690 clang::CodeGen::CodeGenFunction::EmitLValue(clang::Expr const*, clang::CodeGen::KnownNonNull_t) (/home/jhuber/Documents/llvm/clang/lib/libclangCodeGen.so.17git+0x508690)
#11 0x00007f0013ad683d (anonymous namespace)::OMPLexicalScope::OMPLexicalScope(clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, std::optional<llvm::omp::Directive>, bool) (.isra.0) CGStmtOpenMP.cpp:0:0
#12 0x00007f0013b03373 clang::CodeGen::CodeGenFunction::EmitOMPTargetDataDirective(clang::OMPTargetDataDirective const&)::'lambda'(clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&)::operator()(clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&) const::'lambda0'(clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&)::operator()(clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&) const CGStmtOpenMP.cpp:0:0
```
It seems that the templated `OMPCaputeredExprDecl` that is captured in the region does not have an accompanying address in the variable map. So, when we try to emit an L-value on it, we fail to get its address because it was never set. I'm unsure what the proper course of action here would be.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWN9v2zgS_mvol0ENmrQt-cEPjh23xSWt0eT2taCokc0tRepIykn2rz-Qkn8ol21vtyhwuCBxZGk4833D4ZCfhPdqbxCXZHZDZpuRaMPBuuXvh7ZANx8VtnxZPh4QKqu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