Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions clang/lib/CIR/CodeGen/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class Address {
public:
Address(mlir::Value pointer, mlir::Type elementType,
clang::CharUnits alignment)
: pointerAndKnownNonNull(pointer, false), elementType(elementType),
alignment(alignment) {
: Address(pointer, elementType, alignment, false) {}

Address(mlir::Value pointer, mlir::Type elementType,
clang::CharUnits alignment, bool pointerAndKnownNonNull)
: pointerAndKnownNonNull(pointer, pointerAndKnownNonNull),
elementType(elementType), alignment(alignment) {
assert(pointer && "Pointer cannot be null");
assert(elementType && "Element type cannot be null");
assert(!alignment.isZero() && "Alignment cannot be zero");
Expand Down Expand Up @@ -80,7 +84,8 @@ class Address {
/// Return address with different alignment, but same pointer and element
/// type.
Address withAlignment(clang::CharUnits newAlignment) const {
return Address(getPointer(), getElementType(), newAlignment);
return Address(getPointer(), getElementType(), newAlignment,
isKnownNonNull());
}

/// Return address with different element type, a bitcast pointer, and
Expand Down Expand Up @@ -139,6 +144,19 @@ class Address {
template <typename OpTy> OpTy getDefiningOp() const {
return mlir::dyn_cast_or_null<OpTy>(getDefiningOp());
}

/// Whether the pointer is known not to be null.
bool isKnownNonNull() const {
assert(isValid() && "Invalid address");
return static_cast<bool>(pointerAndKnownNonNull.getInt());
}

/// Set the non-null bit.
Address setKnownNonNull() {
assert(isValid() && "Invalid address");
pointerAndKnownNonNull.setInt(true);
return *this;
}
};

} // namespace clang::CIRGen
Expand Down
124 changes: 122 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "clang/AST/Expr.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/DiagnosticFrontend.h"
#include "clang/Basic/OperatorKinds.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/MissingFeatures.h"
Expand Down Expand Up @@ -60,6 +61,112 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const CallExpr *e,
return RValue::get(result);
}

/// Emit the conversions required to turn the given value into an
/// integer of the given size.
static mlir::Value emitToInt(CIRGenFunction &cgf, mlir::Value v, QualType t,
cir::IntType intType) {
v = cgf.emitToMemory(v, t);

if (mlir::isa<cir::PointerType>(v.getType()))
return cgf.getBuilder().createPtrToInt(v, intType);

assert(v.getType() == intType);
return v;
}

static mlir::Value emitFromInt(CIRGenFunction &cgf, mlir::Value v, QualType t,
mlir::Type resultType) {
v = cgf.emitFromMemory(v, t);

if (mlir::isa<cir::PointerType>(resultType))
return cgf.getBuilder().createIntToPtr(v, resultType);

assert(v.getType() == resultType);
return v;
}

static Address checkAtomicAlignment(CIRGenFunction &cgf, const CallExpr *e) {
ASTContext &astContext = cgf.getContext();
Address ptr = cgf.emitPointerWithAlignment(e->getArg(0));
unsigned bytes =
mlir::isa<cir::PointerType>(ptr.getElementType())
? astContext.getTypeSizeInChars(astContext.VoidPtrTy).getQuantity()
: cgf.cgm.getDataLayout().getTypeSizeInBits(ptr.getElementType()) /
cgf.cgm.getASTContext().getCharWidth();

unsigned align = ptr.getAlignment().getQuantity();
if (align % bytes != 0) {
DiagnosticsEngine &diags = cgf.cgm.getDiags();
diags.Report(e->getBeginLoc(), diag::warn_sync_op_misaligned);
// Force address to be at least naturally-aligned.
return ptr.withAlignment(CharUnits::fromQuantity(bytes));
}
return ptr;
}

/// Utility to insert an atomic instruction based on Intrinsic::ID
/// and the expression node.
static mlir::Value makeBinaryAtomicValue(
CIRGenFunction &cgf, cir::AtomicFetchKind kind, const CallExpr *expr,
mlir::Type *originalArgType, mlir::Value *emittedArgValue = nullptr,
cir::MemOrder ordering = cir::MemOrder::SequentiallyConsistent) {

QualType type = expr->getType();
QualType ptrType = expr->getArg(0)->getType();

assert(ptrType->isPointerType());
assert(
cgf.getContext().hasSameUnqualifiedType(type, ptrType->getPointeeType()));
assert(cgf.getContext().hasSameUnqualifiedType(type,
expr->getArg(1)->getType()));

Address destAddr = checkAtomicAlignment(cgf, expr);
CIRGenBuilderTy &builder = cgf.getBuilder();
cir::IntType intType =
ptrType->getPointeeType()->isUnsignedIntegerType()
? builder.getUIntNTy(cgf.getContext().getTypeSize(type))
: builder.getSIntNTy(cgf.getContext().getTypeSize(type));
mlir::Value val = cgf.emitScalarExpr(expr->getArg(1));
mlir::Type valueType = val.getType();
val = emitToInt(cgf, val, type, intType);

// This output argument is needed for post atomic fetch operations
// that calculate the result of the operation as return value of
// <binop>_and_fetch builtins. The `AtomicFetch` operation only updates the
// memory location and returns the old value.
if (emittedArgValue) {
*emittedArgValue = val;
*originalArgType = valueType;
}

auto rmwi = cir::AtomicFetchOp::create(
builder, cgf.getLoc(expr->getSourceRange()), destAddr.emitRawPointer(),
val, kind, ordering, false, /* is volatile */
true); /* fetch first */
return emitFromInt(cgf, rmwi->getResult(0), type, valueType);
}

static RValue emitBinaryAtomicPost(CIRGenFunction &cgf,
cir::AtomicFetchKind atomicOpkind,
const CallExpr *e, cir::BinOpKind binopKind,
bool invert = false) {
mlir::Value emittedArgValue;
mlir::Type originalArgType;
clang::QualType typ = e->getType();
mlir::Value result = makeBinaryAtomicValue(
cgf, atomicOpkind, e, &originalArgType, &emittedArgValue);
clang::CIRGen::CIRGenBuilderTy &builder = cgf.getBuilder();
result = cir::BinOp::create(builder, result.getLoc(), binopKind, result,
emittedArgValue);

if (invert)
result = cir::UnaryOp::create(builder, result.getLoc(),
cir::UnaryOpKind::Not, result);

result = emitFromInt(cgf, result, typ, originalArgType);
return RValue::get(result);
}

namespace {
struct WidthAndSignedness {
unsigned width;
Expand Down Expand Up @@ -866,36 +973,50 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__sync_fetch_and_max:
case Builtin::BI__sync_fetch_and_umin:
case Builtin::BI__sync_fetch_and_umax:
cgm.errorNYI(e->getSourceRange(), "__sync_fetch_and_* builtins NYI");
return getUndefRValue(e->getType());
case Builtin::BI__sync_add_and_fetch_1:
case Builtin::BI__sync_add_and_fetch_2:
case Builtin::BI__sync_add_and_fetch_4:
case Builtin::BI__sync_add_and_fetch_8:
case Builtin::BI__sync_add_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::Add, e,
cir::BinOpKind::Add);
case Builtin::BI__sync_sub_and_fetch_1:
case Builtin::BI__sync_sub_and_fetch_2:
case Builtin::BI__sync_sub_and_fetch_4:
case Builtin::BI__sync_sub_and_fetch_8:
case Builtin::BI__sync_sub_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::Sub, e,
cir::BinOpKind::Sub);
case Builtin::BI__sync_and_and_fetch_1:
case Builtin::BI__sync_and_and_fetch_2:
case Builtin::BI__sync_and_and_fetch_4:
case Builtin::BI__sync_and_and_fetch_8:
case Builtin::BI__sync_and_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::And, e,
cir::BinOpKind::And);
case Builtin::BI__sync_or_and_fetch_1:
case Builtin::BI__sync_or_and_fetch_2:
case Builtin::BI__sync_or_and_fetch_4:
case Builtin::BI__sync_or_and_fetch_8:
case Builtin::BI__sync_or_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::Or, e,
cir::BinOpKind::Or);
case Builtin::BI__sync_xor_and_fetch_1:
case Builtin::BI__sync_xor_and_fetch_2:
case Builtin::BI__sync_xor_and_fetch_4:
case Builtin::BI__sync_xor_and_fetch_8:
case Builtin::BI__sync_xor_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::Xor, e,
cir::BinOpKind::Xor);
case Builtin::BI__sync_nand_and_fetch_1:
case Builtin::BI__sync_nand_and_fetch_2:
case Builtin::BI__sync_nand_and_fetch_4:
case Builtin::BI__sync_nand_and_fetch_8:
case Builtin::BI__sync_nand_and_fetch_16:
return emitBinaryAtomicPost(*this, cir::AtomicFetchKind::Nand, e,
cir::BinOpKind::And, true);
case Builtin::BI__sync_val_compare_and_swap_1:
case Builtin::BI__sync_val_compare_and_swap_2:
case Builtin::BI__sync_val_compare_and_swap_4:
Expand Down Expand Up @@ -1022,8 +1143,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
// Finally, store the result using the pointer.
bool isVolatile =
resultArg->getType()->getPointeeType().isVolatileQualified();
builder.createStore(loc, emitToMemory(arithOp.getResult(), resultQTy),
resultPtr, isVolatile);
builder.createStore(loc, arithOp.getResult(), resultPtr, isVolatile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intentional? It seems unrelated.

Copy link
Contributor Author

@HendrikHuebner HendrikHuebner Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this was intentional -- although I'm not entirely sure its correct. The emitToMemory implementation broke a test for the overflow builtins. But given your comment below about not doing the BitInt conversion, I think that was the actual issue.


return RValue::get(arithOp.getOverflow());
}
Expand Down
26 changes: 23 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,30 @@ LValue CIRGenFunction::emitLValueForFieldInitialization(
return makeAddrLValue(v, fieldType, fieldBaseInfo);
}

/// Converts a scalar value from its primary IR type (as returned
/// by ConvertType) to its load/store type.
mlir::Value CIRGenFunction::emitToMemory(mlir::Value value, QualType ty) {
// Bool has a different representation in memory than in registers,
// but in ClangIR, it is simply represented as a cir.bool value.
// This function is here as a placeholder for possible future changes.
if (auto *atomicTy = ty->getAs<AtomicType>())
ty = atomicTy->getValueType();

if (ty->isExtVectorBoolType()) {
cgm.errorNYI("emitToMemory: extVectorBoolType");
}

// Unlike in classic codegen CIR, bools are kept as `cir.bool` and BitInts are
// kept as `cir.int<N>` until further lowering

return value;
}

mlir::Value CIRGenFunction::emitFromMemory(mlir::Value value, QualType ty) {
if (auto *atomicTy = ty->getAs<AtomicType>())
ty = atomicTy->getValueType();

if (ty->isPackedVectorBoolType(getContext())) {
cgm.errorNYI("emitFromMemory: PackedVectorBoolType");
}

return value;
}

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,10 @@ class CIRGenFunction : public CIRGenTypeCache {
/// to conserve the high level information.
mlir::Value emitToMemory(mlir::Value value, clang::QualType ty);

/// EmitFromMemory - Change a scalar value from its memory
/// representation to its value representation.
mlir::Value emitFromMemory(mlir::Value value, clang::QualType ty);

/// Emit a trap instruction, which is used to abort the program in an abnormal
/// way, usually for debugging purposes.
/// \p createNewBlock indicates whether to create a new block for the IR
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CIRGenFunctionInfo.h"
#include "CIRGenModule.h"
#include "mlir/IR/BuiltinTypes.h"

#include "clang/AST/ASTContext.h"
#include "clang/AST/GlobalDecl.h"
Expand Down
Loading