llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang times out compiling simple program. GCC ok.

Open socketpair opened this issue 2 years ago • 11 comments

https://godbolt.org/z/P7K1MTrcb

#include <algorithm>
#include <array>
#include <ranges>
#include <string>
#include <vector>

using namespace std;

namespace {
consteval string gen() { return "xxx"; }
class qwe {
   public:
    consteval qwe(const string& str) : s(str.size()) { str.copy(x.data(), s); }
    constexpr operator const vector<char>() const {
        return {x.cbegin(), x.cbegin() + s};
    }

   private:
    // 1048576 is a clang limit
    array<char, 1048576> x{};
    size_t s;
};

}  // namespace

int fun1() {
    const vector<char> v2{qwe(gen())};

    return v2.size();
}

socketpair avatar Aug 28 '24 12:08 socketpair

@llvm/issue-subscribers-clang-frontend

Author: Коренберг Марк (socketpair)

https://godbolt.org/z/P7K1MTrcb
#include &lt;algorithm&gt;
#include &lt;array&gt;
#include &lt;ranges&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

using namespace std;

namespace {
consteval string gen() { return "xxx"; }
class qwe {
   public:
    consteval qwe(const string&amp; str) : s(str.size()) { str.copy(x.data(), s); }
    constexpr operator const vector&lt;char&gt;() const {
        return {x.cbegin(), x.cbegin() + s};
    }

   private:
    // 1048576 is a clang limit
    array&lt;char, 1048576&gt; x{};
    size_t s;
};

}  // namespace

int fun1() {
    const vector&lt;char&gt; v2{qwe(gen())};

    return v2.size();
}

llvmbot avatar Aug 28 '24 13:08 llvmbot

https://godbolt.org/z/MTMKaeMxd

This example shows that array optimisation is missing somewhere in Clang. GCC even in -O0 does not emit big array

socketpair avatar Aug 28 '24 13:08 socketpair

Crashes with disabled optimizations: https://godbolt.org/z/EeYbaM5s9

tbaederr avatar Aug 28 '24 14:08 tbaederr

The crash look like a recent regression since it does not crash on clang-18: https://godbolt.org/z/7rv9dq6dz

CC @efriedma-quic

shafik avatar Aug 28 '24 15:08 shafik

  store { <{ i8, i8, i8, [1048573 x i8] }> } { <{ i8, i8, i8, [1048573 x i8] }> <{ i8 120, i8 120, i8 120, [1048573 x i8] zeroinitializer }> }, ptr %0, align 8, !dbg !2009

clang generally avoids try to avoid generating code like this; not sure why it's happening here.

efriedma-quic avatar Aug 28 '24 16:08 efriedma-quic

CompoundStmt 0x7ffff5698318
|-DeclStmt 0x7ffff5698008
| `-VarDecl 0x7ffff5695aa8  used v2 'const vector<char>':'const class std::vector<char>' listinit destroyed
|   `-ExprWithCleanups 0x7ffff5697df8 'const vector<char>':'const class std::vector<char>'
|     `-InitListExpr 0x7ffff5697db0 'const vector<char>':'const class std::vector<char>'
|       `-ImplicitCastExpr 0x7ffff5696b60 'const vector<char>':'const class std::vector<char>' <UserDefinedConversion>
|         `-CXXMemberCallExpr 0x7ffff5696b40 'const vector<char>':'const class std::vector<char>'
|           `-MemberExpr 0x7ffff5696b10 '<bound member function type>' .operator vector 0x7ffff56df048
|             `-ImplicitCastExpr 0x7ffff5696af8 'const class (anonymous namespace)::qwe' lvalue <NoOp>
|               `-MaterializeTemporaryExpr 0x7ffff5696ae0 'qwe':'class (anonymous namespace)::qwe' lvalue
|                 `-CXXFunctionalCastExpr 0x7ffff56969d8 'qwe':'class (anonymous namespace)::qwe' functional cast to qwe <ConstructorConversion>
|                   `-ConstantExpr 0x7ffff5696848 'qwe':'class (anonymous namespace)::qwe'
|                     |-value: Struct
|                     | |-field: Struct
|                     | | `-field: Array size=1048576
|                     | |   |-elements: Int 120, Int 120, Int 120, Int 0
|                     | |   |-elements: Int 0, Int 0, Int 0, Int 0
|                     | |   `-filler: 1048568 x Int 0
|                     | `-field: Int 3
|                     `-ExprWithCleanups 0x7ffff5697ff0 'qwe':'class (anonymous namespace)::qwe'
|                       `-CXXConstructExpr 0x7ffff5697fc0 'qwe':'class (anonymous namespace)::qwe' 'void (const string &)'
|                         `-MaterializeTemporaryExpr 0x7ffff5697fa8 'const string':'const class std::basic_string<char>' lvalue
|                           `-ImplicitCastExpr 0x7ffff5697f90 'const string':'const class std::basic_string<char>' <NoOp>
|                             `-CXXBindTemporaryExpr 0x7ffff5697f70 'string':'class std::basic_string<char>' (CXXTemporary 0x7ffff5697f70)
|                               `-CallExpr 0x7ffff5697f48 'string':'class std::basic_string<char>'
|                                 `-ImplicitCastExpr 0x7ffff5697f30 'string (*)(void)' <FunctionToPointerDecay>
|                                   `-DeclRefExpr 0x7ffff5695b98 'string (void)' lvalue Function 0x7ffff56da718 'gen' 'string (void)'
`-ReturnStmt 0x7ffff5698308
  `-ImplicitCastExpr 0x7ffff56982f0 'int' <IntegralCast>
    `-CXXMemberCallExpr 0x7ffff5698298 'size_type':'unsigned long'
      `-MemberExpr 0x7ffff5698040 '<bound member function type>' .size 0x7ffff56f3638
        `-DeclRefExpr 0x7ffff5698020 'const vector<char>':'const class std::vector<char>' lvalue Var 0x7ffff5695aa8 'v2' 'const vector<char>':'const class std::vector<char>'

The -ConstantExpr 0x7ffff5696848 'qwe':'class (anonymous namespace)::qwe' will be emitted as a Constant, and then a store inst will be created for it.

Does CreateCoercedStore need to use memset to skip zero elements?

tcwzxx avatar Sep 11 '24 03:09 tcwzxx

I assume you're referring to the call to CreateCoercedStore from AggExprEmitter::VisitConstantExpr.

In the general case, you have a constant of arbitrary size where the bits have an arbitrary pattern, so there isn't any reasonable way to emit "optimized" code; you need to just create a global constant and memcpy from it, like AggExprEmitter::EmitArrayInit does. (For example, you can write a loop like for (int i = 0; i < sizeof(x); ++i) x[i] = i; in the constructor for qwe.) This transform doesn't belong in CreateCoercedStore because CreateCoercedStore needs to handle non-constant values.

Of course, there are various special cases you can optimize.

efriedma-quic avatar Sep 12 '24 20:09 efriedma-quic

#include <array>
#include <vector>

using namespace std;

namespace {
struct qwe {
    consteval qwe() = default;
    constexpr operator const vector<unsigned char>() const {
        return {x.cbegin(), x.cbegin()};
    }

    // 1048576 is a clang limit
    // 65535 works, but generate too long source code in asm
    array<unsigned char, 65536> x{};
};

}  // namespace

int fun1() {
    const vector<unsigned char> v2{qwe()};
    return 0;
}

Without optimizations: compiler crashes With -O3: compile hangs

socketpair avatar Jul 12 '25 00:07 socketpair

@efriedma-quic hi, what is the status of the bug I reported ?

socketpair avatar Nov 05 '25 15:11 socketpair

This is still waiting for someone to write a patch.

efriedma-quic avatar Nov 05 '25 18:11 efriedma-quic

Still times out on clang 22.1.8, 23.1.4, and Apple clang 21.0.0 (arm64-apple-darwin), but the slow part is not the frontend — it's the middle end, and it reproduces with plain opt -O2. Timings on the original snippet (timeout-wrapped):

clang++-22 -std=c++23 -O0 -c : 15.4s (ok)
clang++-22 -std=c++23 -O2 -c : >120s, killed
clang++-23 -std=c++23 -O0 -c : 15.1s (ok, and the program is correct: fun1()==3)
clang++-23 -std=c++23 -O2 -c : >120s, killed
g++-16     -std=c++23 -O2 -c : 0.83s

Phase isolation: -O2 -Xclang -disable-llvm-passes -S -emit-llvm finishes in ~2s and produces only 1998 lines of IR; feeding that IR to opt -O2 reproduces the hang. --debug-pass-manager shows where the instruction count explodes:

Running pass: SROAPass on _Z4fun1v (15 instructions)
Running pass: EarlyCSEPass on _Z4fun1v (2097166 instructions)
Running pass: InstCombinePass on _Z4fun1v (2097164 instructions)

After the constexpr conversion operator is inlined into fun1, SROA slices the 1048584-byte literal aggregate store at byte granularity: a standalone opt -passes=sroa expands the single whole-object store into 1,048,578 store i8 instructions (~2.1M instructions in the function overall), and InstCombine plus the AA-based passes then grind on that — that's where the minutes go. On -O0-generated IR the conversion operator is noinline optnone, so opt -O2 on that IR finishes quickly, which is why -O0 compiles fine. Scaling with reduced headers (<array>/<string>/<vector> only, so <ranges> is irrelevant): -O2 array size 4096 → 0.7s, 32768 → 7.3s, 131072 → >30s; -O0 handles 1048576 in ~15s.

Bonus data point: skipping the IR passes entirely segfaults in the backend instead — -O2 -Xclang -disable-llvm-passes -c dies in SelectionDAGBuilder::visitStore → SelectionDAG::getMergeValues recursion during AArch64 instruction selection of fun1, i.e. the same giant-store IR form is pathological in two downstream consumers.

So this is a middle-end/backend robustness issue: SROA (and SelectionDAG, without IR passes) should not expand a whole-object constant store of ~1MB into per-element operations. Until then -O0 gets a correct binary in ~15s, and GCC 16 does the whole thing in under a second at -O2 — in line with the observation upthread that GCC doesn't materialize the big array even at -O0.

jjdiong0-ui avatar Dec 01 '26 04:12 jjdiong0-ui