|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// A helper class for emitting expressions and values as cir::ConstantOp |
| 10 | +// and as initializers for global variables. |
| 11 | +// |
| 12 | +// Note: this is based on clang's LLVM IR codegen in ConstantEmitter.h, reusing |
| 13 | +// this class interface makes it easier move forward with bringing CIR codegen |
| 14 | +// to completion. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef CLANG_LIB_CIR_CODEGEN_CIRGENCONSTANTEMITTER_H |
| 19 | +#define CLANG_LIB_CIR_CODEGEN_CIRGENCONSTANTEMITTER_H |
| 20 | + |
| 21 | +#include "CIRGenFunction.h" |
| 22 | +#include "CIRGenModule.h" |
| 23 | +#include "llvm/ADT/SmallVector.h" |
| 24 | + |
| 25 | +namespace clang::CIRGen { |
| 26 | + |
| 27 | +class ConstantEmitter { |
| 28 | +public: |
| 29 | + CIRGenModule &cgm; |
| 30 | + const CIRGenFunction *cgf; |
| 31 | + |
| 32 | +private: |
| 33 | + bool abstract = false; |
| 34 | + |
| 35 | + /// Whether we're in a constant context. |
| 36 | + bool inConstantContext = false; |
| 37 | + |
| 38 | +public: |
| 39 | + /// Initialize this emission in the context of the given function. |
| 40 | + /// Use this if the expression might contain contextual references like |
| 41 | + /// block addresses or PredefinedExprs. |
| 42 | + ConstantEmitter(CIRGenFunction &cgf) : cgm(cgf.cgm), cgf(&cgf) {} |
| 43 | + |
| 44 | + ConstantEmitter(const ConstantEmitter &other) = delete; |
| 45 | + ConstantEmitter &operator=(const ConstantEmitter &other) = delete; |
| 46 | + |
| 47 | + // All of the "abstract" emission methods below permit the emission to |
| 48 | + // be immediately discarded without finalizing anything. Therefore, they |
| 49 | + // must also promise not to do anything that will, in the future, require |
| 50 | + // finalization: |
| 51 | + // |
| 52 | + // - using the CGF (if present) for anything other than establishing |
| 53 | + // semantic context; for example, an expression with ignored |
| 54 | + // side-effects must not be emitted as an abstract expression |
| 55 | + // |
| 56 | + // - doing anything that would not be safe to duplicate within an |
| 57 | + // initializer or to propagate to another context; for example, |
| 58 | + // side effects, or emitting an initialization that requires a |
| 59 | + // reference to its current location. |
| 60 | + mlir::Attribute emitForMemory(mlir::Attribute c, QualType t); |
| 61 | + |
| 62 | + /// Emit the result of the given expression as an abstract constant, |
| 63 | + /// asserting that it succeeded. This is only safe to do when the |
| 64 | + /// expression is known to be a constant expression with either a fairly |
| 65 | + /// simple type or a known simple form. |
| 66 | + mlir::Attribute emitAbstract(SourceLocation loc, const APValue &value, |
| 67 | + QualType t); |
| 68 | + |
| 69 | + mlir::Attribute tryEmitConstantExpr(const ConstantExpr *CE); |
| 70 | + |
| 71 | + // These are private helper routines of the constant emitter that |
| 72 | + // can't actually be private because things are split out into helper |
| 73 | + // functions and classes. |
| 74 | + |
| 75 | + mlir::Attribute tryEmitPrivateForVarInit(const VarDecl &d); |
| 76 | + |
| 77 | + mlir::Attribute tryEmitPrivate(const APValue &value, QualType destType); |
| 78 | + mlir::Attribute tryEmitPrivateForMemory(const APValue &value, QualType t); |
| 79 | + |
| 80 | + /// Try to emit the initializer of the given declaration as an abstract |
| 81 | + /// constant. |
| 82 | + mlir::Attribute tryEmitAbstractForInitializer(const VarDecl &d); |
| 83 | + |
| 84 | +private: |
| 85 | + class AbstractStateRAII { |
| 86 | + ConstantEmitter &emitter; |
| 87 | + bool oldValue; |
| 88 | + |
| 89 | + public: |
| 90 | + AbstractStateRAII(ConstantEmitter &emitter, bool value) |
| 91 | + : emitter(emitter), oldValue(emitter.abstract) { |
| 92 | + emitter.abstract = value; |
| 93 | + } |
| 94 | + ~AbstractStateRAII() { emitter.abstract = oldValue; } |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace clang::CIRGen |
| 99 | + |
| 100 | +#endif // CLANG_LIB_CIR_CODEGEN_CIRGENCONSTANTEMITTER_H |
0 commit comments