[clang] [CIR][NFC] Refactor constant pointer l-value handling (PR #144165)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 13 15:36:40 PDT 2025
================
@@ -329,6 +329,222 @@ emitArrayConstant(CIRGenModule &cgm, mlir::Type desiredType,
return {};
}
+//===----------------------------------------------------------------------===//
+// ConstantLValueEmitter
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// A struct which can be used to peephole certain kinds of finalization
+/// that normally happen during l-value emission.
+struct ConstantLValue {
+ llvm::PointerUnion<mlir::Value, mlir::Attribute> value;
+ bool hasOffsetApplied;
+
+ ConstantLValue(std::nullptr_t) : value(nullptr), hasOffsetApplied(false) {}
+ ConstantLValue() : value(nullptr), hasOffsetApplied(false) {}
+};
+
+/// A helper class for emitting constant l-values.
+class ConstantLValueEmitter
+ : public ConstStmtVisitor<ConstantLValueEmitter, ConstantLValue> {
+ CIRGenModule &cgm;
+ ConstantEmitter &emitter;
+ const APValue &value;
+ QualType destType;
+
+ // Befriend StmtVisitorBase so that we don't have to expose Visit*.
+ friend StmtVisitorBase;
+
+public:
+ ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
+ QualType destType)
+ : cgm(emitter.cgm), emitter(emitter), value(value), destType(destType) {}
+
+ mlir::Attribute tryEmit();
+
+private:
+ mlir::Attribute tryEmitAbsolute(mlir::Type destTy);
+ ConstantLValue tryEmitBase(const APValue::LValueBase &base);
+
+ ConstantLValue VisitStmt(const Stmt *s) { return nullptr; }
+ ConstantLValue VisitConstantExpr(const ConstantExpr *e);
+ ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *e);
+ ConstantLValue VisitStringLiteral(const StringLiteral *e);
+ ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *e);
+ ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *e);
+ ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *e);
+ ConstantLValue VisitPredefinedExpr(const PredefinedExpr *e);
+ ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *e);
+ ConstantLValue VisitCallExpr(const CallExpr *e);
+ ConstantLValue VisitBlockExpr(const BlockExpr *e);
+ ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *e);
+ ConstantLValue
+ VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *e);
+};
+
+} // namespace
+
+mlir::Attribute ConstantLValueEmitter::tryEmit() {
+ const APValue::LValueBase &base = value.getLValueBase();
+
+ // The destination type should be a pointer or reference
+ // type, but it might also be a cast thereof.
+ //
+ // FIXME: the chain of casts required should be reflected in the APValue.
+ // We need this in order to correctly handle things like a ptrtoint of a
+ // non-zero null pointer and addrspace casts that aren't trivially
+ // represented in LLVM IR.
+ mlir::Type destTy = cgm.getTypes().convertTypeForMem(destType);
+ assert(mlir::isa<cir::PointerType>(destTy));
+
+ // If there's no base at all, this is a null or absolute pointer,
+ // possibly cast back to an integer type.
+ if (!base)
+ return tryEmitAbsolute(destTy);
+
+ // Otherwise, try to emit the base.
+ ConstantLValue result = tryEmitBase(base);
+
+ // If that failed, we're done.
+ llvm::PointerUnion<mlir::Value, mlir::Attribute> &value = result.value;
+ if (!value)
+ return {};
+
+ // Apply the offset if necessary and not already done.
+ if (!result.hasOffsetApplied) {
+ cgm.errorNYI("ConstantLValueEmitter: apply offset");
+ return {};
+ }
+
+ // Convert to the appropriate type; this could be an lvalue for
+ // an integer. FIXME: performAddrSpaceCast
+ if (mlir::isa<cir::PointerType>(destTy)) {
+ if (auto attr = mlir::dyn_cast<mlir::Attribute>(value))
+ return attr;
+ cgm.errorNYI("ConstantLValueEmitter: non-attribute pointer");
+ return {};
+ }
+
+ cgm.errorNYI("ConstantLValueEmitter: other?");
+ return {};
+}
+
+/// Try to emit an absolute l-value, such as a null pointer or an integer
+/// bitcast to pointer type.
+mlir::Attribute ConstantLValueEmitter::tryEmitAbsolute(mlir::Type destTy) {
+ // If we're producing a pointer, this is easy.
+ auto destPtrTy = mlir::cast<cir::PointerType>(destTy);
+ return cgm.getBuilder().getConstPtrAttr(
+ destPtrTy, value.getLValueOffset().getQuantity());
+}
+
+ConstantLValue
+ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
+ // Handle values.
+ if (const ValueDecl *d = base.dyn_cast<const ValueDecl *>()) {
+ // The constant always points to the canonical declaration. We want to look
+ // at properties of the most recent declaration at the point of emission.
+ d = cast<ValueDecl>(d->getMostRecentDecl());
+
+ if (d->hasAttr<WeakRefAttr>()) {
+ cgm.errorNYI(d->getSourceRange(),
+ "ConstantLValueEmitter: emit pointer base for weakref");
+ return {};
+ }
+
+ if (auto *fd = dyn_cast<FunctionDecl>(d)) {
+ cgm.errorNYI(fd->getSourceRange(),
+ "ConstantLValueEmitter: function decl");
+ return {};
+ }
+
+ if (auto *vd = dyn_cast<VarDecl>(d)) {
+ cgm.errorNYI(vd->getSourceRange(), "ConstantLValueEmitter: var decl");
+ return {};
+ }
+ }
+
+ // Handle typeid(T).
+ if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) {
----------------
AmrDeveloper wrote:
NIT: `TI` is unused
```
if (base.dyn_cast<TypeInfoLValue>()) {
}
```
https://github.com/llvm/llvm-project/pull/144165
More information about the cfe-commits
mailing list