Constexpr Evaluation and CodeGen Questions

Not sure this is the best section of the forum to post these questions so I’m happy for them to be moved if there’s a better location!

I have a couple of beginner questions relating to how constexpr works in Clang/LLVM if someone is able to answer I’d greatly appreciate that.

Is the purpose of the Evaluator/s inside of ExprConstant.cpp largely to verify that something doesn’t violate constexpr rules and the result is discarded?

The CodeGen for constexpr inside of CGExprConstant.cpp seems to only handle certain cases (arrays/structs/unions), are cases like compile time computations (such as those found in some of the algorithms from the standard library e.g. transform/reduce etc.) handled by optimization passes?

1 Like

CGExprConstant.cpp handles all of the constant-evaluation we do in emitted code, but it largely does so by deferring to the AST-level constant evaluator and then translating the resulting CGValue to an llvm::Constant*. Most of the code in CGExprConstant.cpp is about aggregates because that’s the main source of complexity in that mapping; the easy cases like integers are handled with one-liners in ConstantEmitter::tryEmitPrivate.

There are also a few cases that CGExprConstant.cpp tries to handle that the formal constant-evaluator doesn’t really have logic for.

1 Like

Thanks very much, that helped me quite a bit! I believe I have a better idea of how it works now.