<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/65138>65138</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP] Array reductions with non-constant sizes fail in code gen
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          doru1004
      </td>
    </tr>
</table>

<pre>
    For the following program:

```
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define SIZE (1024)

int test(int s)
{
  int buf[SIZE];
  int *counter = (int*)malloc(s * sizeof(int));
  int *another_counter = (int*)malloc(s * sizeof(int));
  for (int i = 0; i < SIZE; i++)
    buf[i] = i % s;

#pragma omp target teams distribute parallel for  map(tofrom: buf[0:SIZE]) reduction(+:counter[:s])
    for (int i =0; i < SIZE; i++)
    {
       int b = buf[i];
 counter[b] += 1;
    }

  printf("buf[5] = %d red = %d, counter[5] = %d\n", buf[5], red, counter[5]);
  return buf[5];
}

int main()
{
  test (17);
  return 0;
}

```

compilation fails:

```
clang-16: /home/dobercea/upstream/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:2206: clang::CodeGen::CodeGenFunction::VlaSizePair clang::CodeGen::CodeGenFunction::getVLASize(const clang::VariableArrayType*): Assertion `vlaSize && "no size for VLA!"' failed.
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:
1.      test.cpp:27:1: current parser token 'int'
2. test.cpp:6:5: LLVM IR generation of declaration 'test'
 #0 0x000056429091e474 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000056429091b804 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f8d9dc55420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f8d9d6f200b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #4 0x00007f8d9d6d1859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #5 0x00007f8d9d6d1729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #6 0x00007f8d9d6d1729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
 #7 0x00007f8d9d6e2fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #8 0x0000564290cb4677 clang::CodeGen::CodeGenFunction::getVLASize(clang::VariableArrayType const*) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2afc677)
 #9 0x0000564290cb501a clang::CodeGen::CodeGenFunction::emitArrayLength(clang::ArrayType const*, clang::QualType&, clang::CodeGen::Address&) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2afd01a)
#10 0x000056429104e276 clang::CodeGen::CodeGenFunction::EmitAggregateCopy(clang::CodeGen::LValue, clang::CodeGen::LValue, clang::QualType, clang::CodeGen::AggValueSlot::Overlap_t, bool) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2e96276)
#11 0x00005642911975e7 emitReductionListCopy((anonymous namespace)::CopyAction, clang::CodeGen::CodeGenFunction&, clang::QualType, llvm::ArrayRef<clang::Expr const*>, clang::CodeGen::Address, clang::CodeGen::Address, CopyOptionsTy) (.isra.0.constprop.0) CGOpenMPRuntimeGPU.cpp:0:0
#12 0x00005642911984f0 emitShuffleAndReduceFunction(clang::CodeGen::CodeGenModule&, llvm::ArrayRef<clang::Expr const*>, clang::QualType, llvm::Function*, clang::SourceLocation) CGOpenMPRuntimeGPU.cpp:0:0
#13 0x000056429119f8d1 clang::CodeGen::CGOpenMPRuntimeGPU::emitReduction(clang::CodeGen::CodeGenFunction&, clang::SourceLocation, llvm::ArrayRef<clang::Expr const*>, llvm::ArrayRef<clang::Expr const*>, llvm::ArrayRef<clang::Expr const*>, llvm::ArrayRef<clang::Expr const*>, clang::CodeGen::CGOpenMPRuntime::ReductionOptionsTy) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2fe78d1)
#14 0x0000564290c5791a clang::CodeGen::CodeGenFunction::EmitOMPReductionClauseFinal(clang::OMPExecutableDirective const&, llvm::omp::Directive) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2a9f91a)
#15 0x0000564290c85035 clang::CodeGen::CodeGenFunction::EmitOMPWorksharingLoop(clang::OMPLoopDirective const&, clang::Expr*, llvm::function_ref<std::pair<clang::CodeGen::LValue, clang::CodeGen::LValue> (clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&)> const&, llvm::function_ref<std::pair<llvm::Value*, llvm::Value*> (clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, clang::CodeGen::Address, clang::CodeGen::Address)> const&) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2acd035)
#16 0x0000564290c85e98 void clang::CodeGen::RegionCodeGenTy::CallbackFn<emitInnerParallelForWhenCombined(clang::CodeGen::CodeGenFunction&, clang::OMPLoopDirective const&, clang::CodeGen::CodeGenFunction::JumpDest)::'lambda'(clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&)>(long, clang::CodeGen::CodeGenFunction&, clang::CodeGen::PrePostActionTy&) CGStmtOpenMP.cpp:0:0
#17 0x000056429112c68d clang::CodeGen::RegionCodeGenTy::operator()(clang::CodeGen::CodeGenFunction&) const (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2f7468d)
#18 0x000056429112c9ec (anonymous namespace)::CGOpenMPRegionInfo::EmitBody(clang::CodeGen::CodeGenFunction&, clang::Stmt const*) CGOpenMPRuntime.cpp:0:0
#19 0x0000564290c97e9b clang::CodeGen::CodeGenFunction::GenerateOpenMPCapturedStmtFunction(clang::CapturedStmt const&, clang::SourceLocation) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2adfe9b)
#20 0x000056429116545a emitParallelOrTeamsOutlinedFunction(clang::CodeGen::CodeGenModule&, clang::OMPExecutableDirective const&, clang::CapturedStmt const*, clang::VarDecl const*, llvm::omp::Directive, llvm::StringRef, clang::CodeGen::RegionCodeGenTy const&) CGOpenMPRuntime.cpp:0:0
#21 0x00005642911658df clang::CodeGen::CGOpenMPRuntime::emitParallelOutlinedFunction(clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, clang::VarDecl const*, llvm::omp::Directive, clang::CodeGen::RegionCodeGenTy const&) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2fad8df)
#22 0x000056429119ccf7 clang::CodeGen::CGOpenMPRuntimeGPU::emitParallelOutlinedFunction(clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, clang::VarDecl const*, llvm::omp::Directive, clang::CodeGen::RegionCodeGenTy const&) (/home/dobercea/rocm/trunk_1.0/bin/clang-16+0x2fe4cf7)
#23 0x0000564290c78363 emitCommonOMPParallelDirective(clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, llvm::omp::Directive, clang::CodeGen::RegionCodeGenTy const&, llvm::function_ref<void (clang::CodeGen::CodeGenFunction&, clang::OMPExecutableDirective const&, llvm::SmallVectorImpl<llvm::Value*>&)> const&) CGStmtOpenMP.cpp:0:0
#24 0x0000564290c78954 emitInnerParallelForWhenCombined(clang::CodeGen::CodeGenFunction&, clang::OMPLoopDirective const&, clang::CodeGen::CodeGenFunction::JumpDest) CGStmtOpenMP.cpp:0:0
```


It looks like the `counter` variable doesn't have a size when it is a reduction variable but this could well be a red herring:

```
VarDecl 0x564298bbdd50 <test.cpp:15:88> col:88 implicit used counter 'int[<null expr>]' cinit
|-IntegerLiteral 0x564298bbde58 <col:88> 'int' 0
`-OMPReferencedVarAttr 0x564298bbddb8 <<invalid sloc>> Implicit
  `-DeclRefExpr 0x564298bbdb20 <col:88> 'int *' lvalue Var 0x564298bbb0f8 'counter' 'int *'
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJ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