Skip to content

Commit

Permalink
[CIR][CIRGen][NFC] Update buildPointerWithAlignment with LLVM upstrea…
Browse files Browse the repository at this point in the history
…m codegen approach
  • Loading branch information
bcardosolopes authored and lanza committed Nov 2, 2024
1 parent b33d406 commit df17de9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
20 changes: 17 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,25 @@ static Address buildPointerWithAlignment(const Expr *E,
}
}

// std::addressof and variants.
if (auto *Call = dyn_cast<CallExpr>(E)) {
switch (Call->getBuiltinCallee()) {
default:
break;
case Builtin::BIaddressof:
case Builtin::BI__addressof:
case Builtin::BI__builtin_addressof: {
llvm_unreachable("NYI");
}
}
}

// TODO: conditional operators, comma.

// Otherwise, use the alignment of the type.
CharUnits Align =
CGF.CGM.getNaturalPointeeTypeAlignment(E->getType(), BaseInfo);
return Address(CGF.buildScalarExpr(E), Align);
return CGF.makeNaturalAddressForPointer(
CGF.buildScalarExpr(E), E->getType()->getPointeeType(), CharUnits(),
/*ForPointeeType=*/true, BaseInfo, IsKnownNonNull);
}

/// Helper method to check if the underlying ABI is AAPCS
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,19 @@ class CIRGenFunction : public CIRGenTypeCache {
LValue MakeNaturalAlignPointeeAddrLValue(mlir::Value V, clang::QualType T);
LValue MakeNaturalAlignAddrLValue(mlir::Value V, QualType T);

/// Construct an address with the natural alignment of T. If a pointer to T
/// is expected to be signed, the pointer passed to this function must have
/// been signed, and the returned Address will have the pointer authentication
/// information needed to authenticate the signed pointer.
Address makeNaturalAddressForPointer(
mlir::Value Ptr, QualType T, CharUnits Alignment = CharUnits::Zero(),
bool ForPointeeType = false, LValueBaseInfo *BaseInfo = nullptr,
KnownNonNull_t IsKnownNonNull = NotKnownNonNull) {
if (Alignment.isZero())
Alignment = CGM.getNaturalTypeAlignment(T, BaseInfo, ForPointeeType);
return Address(Ptr, convertTypeForMem(T), Alignment, IsKnownNonNull);
}

/// Load the value for 'this'. This function is only valid while generating
/// code for an C++ member function.
/// FIXME(cir): this should return a mlir::Value!
Expand Down

0 comments on commit df17de9

Please sign in to comment.