Skip to content

Commit 5034550

Browse files
bcardosolopeslanza
authored andcommitted
Revert "[CIR][CIRGen] CIR generation for bitfields. Fixes #13 (#233)"
Breaks ninja check-clang-cir This reverts commit 471e568.
1 parent 4275820 commit 5034550

10 files changed

+85
-563
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,6 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
460460
return getConstInt(
461461
loc, t, isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
462462
}
463-
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
464-
const llvm::APInt &val) {
465-
return create<mlir::cir::ConstantOp>(loc, typ,
466-
getAttr<mlir::cir::IntAttr>(typ, val));
467-
}
468463
mlir::cir::ConstantOp getBool(bool state, mlir::Location loc) {
469464
return create<mlir::cir::ConstantOp>(loc, getBoolTy(),
470465
getCIRBoolAttr(state));
@@ -682,65 +677,6 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
682677
mlir::cir::UnaryOpKind::Not, value);
683678
}
684679

685-
mlir::Value createBinop(mlir::Value lhs, mlir::cir::BinOpKind kind,
686-
const llvm::APInt &rhs) {
687-
return create<mlir::cir::BinOp>(
688-
lhs.getLoc(), lhs.getType(), kind, lhs,
689-
getConstAPInt(lhs.getLoc(), lhs.getType(), rhs));
690-
}
691-
692-
mlir::Value createBinop(mlir::Value lhs, mlir::cir::BinOpKind kind,
693-
mlir::Value rhs) {
694-
return create<mlir::cir::BinOp>(lhs.getLoc(), lhs.getType(), kind, lhs,
695-
rhs);
696-
}
697-
698-
mlir::Value createShift(mlir::Value lhs, const llvm::APInt &rhs,
699-
bool isShiftLeft) {
700-
return create<mlir::cir::ShiftOp>(
701-
lhs.getLoc(), lhs.getType(), lhs,
702-
getConstAPInt(lhs.getLoc(), lhs.getType(), rhs), isShiftLeft);
703-
}
704-
705-
mlir::Value createShift(mlir::Value lhs, unsigned bits, bool isShiftLeft) {
706-
auto width = lhs.getType().dyn_cast<mlir::cir::IntType>().getWidth();
707-
auto shift = llvm::APInt(width, bits);
708-
return createShift(lhs, shift, isShiftLeft);
709-
}
710-
711-
mlir::Value createShiftLeft(mlir::Value lhs, unsigned bits) {
712-
return createShift(lhs, bits, true);
713-
}
714-
715-
mlir::Value createShiftRight(mlir::Value lhs, unsigned bits) {
716-
return createShift(lhs, bits, false);
717-
}
718-
719-
mlir::Value createLowBitsSet(mlir::Location loc, unsigned size,
720-
unsigned bits) {
721-
auto val = llvm::APInt::getLowBitsSet(size, bits);
722-
auto typ = mlir::cir::IntType::get(getContext(), size, false);
723-
return getConstAPInt(loc, typ, val);
724-
}
725-
726-
mlir::Value createAnd(mlir::Value lhs, llvm::APInt rhs) {
727-
auto val = getConstAPInt(lhs.getLoc(), lhs.getType(), rhs);
728-
return createBinop(lhs, mlir::cir::BinOpKind::And, val);
729-
}
730-
731-
mlir::Value createAnd(mlir::Value lhs, mlir::Value rhs) {
732-
return createBinop(lhs, mlir::cir::BinOpKind::And, rhs);
733-
}
734-
735-
mlir::Value createOr(mlir::Value lhs, llvm::APInt rhs) {
736-
auto val = getConstAPInt(lhs.getLoc(), lhs.getType(), rhs);
737-
return createBinop(lhs, mlir::cir::BinOpKind::Or, val);
738-
}
739-
740-
mlir::Value createOr(mlir::Value lhs, mlir::Value rhs) {
741-
return createBinop(lhs, mlir::cir::BinOpKind::Or, rhs);
742-
}
743-
744680
//===--------------------------------------------------------------------===//
745681
// Cast/Conversion Operators
746682
//===--------------------------------------------------------------------===//
@@ -791,5 +727,6 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
791727
return createCast(mlir::cir::CastKind::bitcast, src, newTy);
792728
}
793729
};
730+
794731
} // namespace cir
795732
#endif

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 12 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "clang/AST/GlobalDecl.h"
2222
#include "clang/Basic/Builtins.h"
2323
#include "clang/CIR/Dialect/IR/CIRDialect.h"
24-
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
2524
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2625
#include "llvm/Support/Casting.h"
2726
#include "llvm/Support/ErrorHandling.h"
@@ -129,7 +128,6 @@ static Address buildPointerWithAlignment(const Expr *E,
129128
if (PtrTy->getPointeeType()->isVoidType())
130129
break;
131130
assert(!UnimplementedFeature::tbaa());
132-
133131
LValueBaseInfo InnerBaseInfo;
134132
Address Addr = CGF.buildPointerWithAlignment(
135133
CE->getSubExpr(), &InnerBaseInfo, IsKnownNonNull);
@@ -213,78 +211,13 @@ static Address buildPointerWithAlignment(const Expr *E,
213211
return Address(CGF.buildScalarExpr(E), Align);
214212
}
215213

216-
/// Helper method to check if the underlying ABI is AAPCS
217-
static bool isAAPCS(const TargetInfo &TargetInfo) {
218-
return TargetInfo.getABI().starts_with("aapcs");
219-
}
220-
221-
Address CIRGenFunction::getAddrOfField(LValue base, const FieldDecl *field,
222-
unsigned index) {
223-
if (index == 0)
224-
return base.getAddress();
225-
226-
auto loc = getLoc(field->getLocation());
227-
auto fieldType = convertType(field->getType());
228-
auto fieldPtr =
229-
mlir::cir::PointerType::get(getBuilder().getContext(), fieldType);
230-
auto sea = getBuilder().createGetMember(
231-
loc, fieldPtr, base.getPointer(), field->getName(), index);
232-
233-
return Address(sea, CharUnits::One());
234-
}
235-
236-
static bool useVolatileForBitField(const CIRGenModule &cgm, LValue base,
237-
const CIRGenBitFieldInfo &info,
238-
const FieldDecl *field) {
239-
return isAAPCS(cgm.getTarget()) && cgm.getCodeGenOpts().AAPCSBitfieldWidth &&
240-
info.VolatileStorageSize != 0 &&
241-
field->getType()
242-
.withCVRQualifiers(base.getVRQualifiers())
243-
.isVolatileQualified();
244-
}
245-
246-
LValue CIRGenFunction::buildLValueForBitField(LValue base,
247-
const FieldDecl *field) {
248-
249-
LValueBaseInfo BaseInfo = base.getBaseInfo();
250-
const RecordDecl *rec = field->getParent();
251-
auto &layout = CGM.getTypes().getCIRGenRecordLayout(field->getParent());
252-
auto &info = layout.getBitFieldInfo(field);
253-
auto useVolatile = useVolatileForBitField(CGM, base, info, field);
254-
unsigned Idx = layout.getCIRFieldNo(field);
255-
256-
if (useVolatile ||
257-
(IsInPreservedAIRegion ||
258-
(getDebugInfo() && rec->hasAttr<BPFPreserveAccessIndexAttr>()))) {
259-
llvm_unreachable("NYI");
260-
}
261-
262-
Address Addr = getAddrOfField(base, field, Idx);
263-
264-
const unsigned SS = useVolatile ? info.VolatileStorageSize : info.StorageSize;
265-
266-
// Get the access type.
267-
mlir::Type FieldIntTy = builder.getUIntNTy(SS);
268-
269-
auto loc = getLoc(field->getLocation());
270-
if (Addr.getElementType() != FieldIntTy)
271-
Addr = builder.createElementBitCast(loc, Addr, FieldIntTy);
272-
273-
QualType fieldType =
274-
field->getType().withCVRQualifiers(base.getVRQualifiers());
275-
276-
assert(!UnimplementedFeature::tbaa() && "NYI TBAA for bit fields");
277-
LValueBaseInfo FieldBaseInfo(BaseInfo.getAlignmentSource());
278-
return LValue::MakeBitfield(Addr, info, fieldType, FieldBaseInfo);
279-
}
280-
281214
LValue CIRGenFunction::buildLValueForField(LValue base,
282215
const FieldDecl *field) {
283-
284216
LValueBaseInfo BaseInfo = base.getBaseInfo();
285217

286-
if (field->isBitField())
287-
return buildLValueForBitField(base, field);
218+
if (field->isBitField()) {
219+
llvm_unreachable("NYI");
220+
}
288221

289222
// Fields of may-alias structures are may-alais themselves.
290223
// FIXME: this hould get propagated down through anonymous structs and unions.
@@ -583,55 +516,12 @@ void CIRGenFunction::buildStoreOfScalar(mlir::Value value, LValue lvalue,
583516
/// method emits the address of the lvalue, then loads the result as an rvalue,
584517
/// returning the rvalue.
585518
RValue CIRGenFunction::buildLoadOfLValue(LValue LV, SourceLocation Loc) {
519+
assert(LV.isSimple() && "not implemented");
586520
assert(!LV.getType()->isFunctionType());
587521
assert(!(LV.getType()->isConstantMatrixType()) && "not implemented");
588522

589-
if (LV.isBitField())
590-
return buildLoadOfBitfieldLValue(LV, Loc);
591-
592-
if (LV.isSimple())
593-
return RValue::get(buildLoadOfScalar(LV, Loc));
594-
llvm_unreachable("NYI");
595-
}
596-
597-
RValue CIRGenFunction::buildLoadOfBitfieldLValue(LValue LV,
598-
SourceLocation Loc) {
599-
const CIRGenBitFieldInfo &Info = LV.getBitFieldInfo();
600-
601-
// Get the output type.
602-
mlir::Type ResLTy = convertType(LV.getType());
603-
Address Ptr = LV.getBitFieldAddress();
604-
mlir::Value Val = builder.createLoad(getLoc(Loc), Ptr);
605-
auto ValWidth = Val.getType().cast<IntType>().getWidth();
606-
607-
bool UseVolatile = LV.isVolatileQualified() &&
608-
Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
609-
const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
610-
const unsigned StorageSize =
611-
UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
612-
613-
if (Info.IsSigned) {
614-
assert(static_cast<unsigned>(Offset + Info.Size) <= StorageSize);
615-
616-
mlir::Type typ = builder.getSIntNTy(ValWidth);
617-
Val = builder.createIntCast(Val, typ);
618-
619-
unsigned HighBits = StorageSize - Offset - Info.Size;
620-
if (HighBits)
621-
Val = builder.createShiftLeft(Val, HighBits);
622-
if (Offset + HighBits)
623-
Val = builder.createShiftRight(Val, Offset + HighBits);
624-
} else {
625-
if (Offset)
626-
Val = builder.createShiftRight(Val, Offset);
627-
628-
if (static_cast<unsigned>(Offset) + Info.Size < StorageSize)
629-
Val = builder.createAnd(Val,
630-
llvm::APInt::getLowBitsSet(ValWidth, Info.Size));
631-
}
632-
Val = builder.createIntCast(Val, ResLTy);
633-
assert(!UnimplementedFeature::emitScalarRangeCheck() && "NYI");
634-
return RValue::get(Val);
523+
// Everything needs a load.
524+
return RValue::get(buildLoadOfScalar(LV, Loc));
635525
}
636526

637527
void CIRGenFunction::buildStoreThroughLValue(RValue Src, LValue Dst) {
@@ -654,81 +544,6 @@ void CIRGenFunction::buildStoreThroughLValue(RValue Src, LValue Dst) {
654544
buildStoreOfScalar(Src.getScalarVal(), Dst);
655545
}
656546

657-
void CIRGenFunction::buildStoreThroughBitfieldLValue(RValue Src, LValue Dst,
658-
mlir::Value &Result) {
659-
const CIRGenBitFieldInfo &Info = Dst.getBitFieldInfo();
660-
mlir::Type ResLTy = getTypes().convertTypeForMem(Dst.getType());
661-
Address Ptr = Dst.getBitFieldAddress();
662-
663-
// Get the source value, truncated to the width of the bit-field.
664-
mlir::Value SrcVal = Src.getScalarVal();
665-
666-
// Cast the source to the storage type and shift it into place.
667-
SrcVal = builder.createIntCast(SrcVal, Ptr.getElementType());
668-
auto SrcWidth = SrcVal.getType().cast<IntType>().getWidth();
669-
mlir::Value MaskedVal = SrcVal;
670-
671-
const bool UseVolatile =
672-
CGM.getCodeGenOpts().AAPCSBitfieldWidth && Dst.isVolatileQualified() &&
673-
Info.VolatileStorageSize != 0 && isAAPCS(CGM.getTarget());
674-
const unsigned StorageSize =
675-
UseVolatile ? Info.VolatileStorageSize : Info.StorageSize;
676-
const unsigned Offset = UseVolatile ? Info.VolatileOffset : Info.Offset;
677-
// See if there are other bits in the bitfield's storage we'll need to load
678-
// and mask together with source before storing.
679-
if (StorageSize != Info.Size) {
680-
assert(StorageSize > Info.Size && "Invalid bitfield size.");
681-
682-
mlir::Value Val = buildLoadOfScalar(Dst, Dst.getPointer().getLoc());
683-
684-
// Mask the source value as needed.
685-
if (!hasBooleanRepresentation(Dst.getType()))
686-
SrcVal = builder.createAnd(
687-
SrcVal, llvm::APInt::getLowBitsSet(SrcWidth, Info.Size));
688-
689-
MaskedVal = SrcVal;
690-
if (Offset)
691-
SrcVal = builder.createShiftLeft(SrcVal, Offset);
692-
693-
// Mask out the original value.
694-
Val = builder.createAnd(
695-
Val, ~llvm::APInt::getBitsSet(SrcWidth, Offset, Offset + Info.Size));
696-
697-
// Or together the unchanged values and the source value.
698-
SrcVal = builder.createOr(Val, SrcVal);
699-
700-
} else {
701-
// According to the AACPS:
702-
// When a volatile bit-field is written, and its container does not overlap
703-
// with any non-bit-field member, its container must be read exactly once
704-
// and written exactly once using the access width appropriate to the type
705-
// of the container. The two accesses are not atomic.
706-
llvm_unreachable("volatile bit-field is not implemented for the AACPS");
707-
}
708-
709-
// Write the new value back out.
710-
// TODO: constant matrix type, volatile, no init, non temporal, TBAA
711-
buildStoreOfScalar(SrcVal, Ptr, Dst.isVolatileQualified(), Dst.getType(),
712-
Dst.getBaseInfo(), false, false);
713-
714-
// Return the new value of the bit-field.
715-
mlir::Value ResultVal = MaskedVal;
716-
ResultVal = builder.createIntCast(ResultVal, ResLTy);
717-
718-
// Sign extend the value if needed.
719-
if (Info.IsSigned) {
720-
assert(Info.Size <= StorageSize);
721-
unsigned HighBits = StorageSize - Info.Size;
722-
723-
if (HighBits) {
724-
ResultVal = builder.createShiftLeft(ResultVal, HighBits);
725-
ResultVal = builder.createShiftRight(ResultVal, HighBits);
726-
}
727-
}
728-
729-
Result = buildFromMemory(ResultVal, Dst.getType());
730-
}
731-
732547
static LValue buildGlobalVarDeclLValue(CIRGenFunction &CGF, const Expr *E,
733548
const VarDecl *VD) {
734549
QualType T = E->getType();
@@ -952,13 +767,7 @@ LValue CIRGenFunction::buildBinaryOperatorLValue(const BinaryOperator *E) {
952767
LValue LV = buildLValue(E->getLHS());
953768

954769
SourceLocRAIIObject Loc{*this, getLoc(E->getSourceRange())};
955-
if (LV.isBitField()) {
956-
mlir::Value result;
957-
buildStoreThroughBitfieldLValue(RV, LV, result);
958-
} else {
959-
buildStoreThroughLValue(RV, LV);
960-
}
961-
770+
buildStoreThroughLValue(RV, LV);
962771
assert(!getContext().getLangOpts().OpenMP &&
963772
"last priv cond not implemented");
964773
return LV;
@@ -2394,13 +2203,6 @@ mlir::Value CIRGenFunction::buildAlloca(StringRef name, QualType ty,
23942203

23952204
mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
23962205
SourceLocation Loc) {
2397-
return buildLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
2398-
lvalue.getType(), getLoc(Loc), lvalue.getBaseInfo(),
2399-
lvalue.isNontemporal());
2400-
}
2401-
2402-
mlir::Value CIRGenFunction::buildLoadOfScalar(LValue lvalue,
2403-
mlir::Location Loc) {
24042206
return buildLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
24052207
lvalue.getType(), Loc, lvalue.getBaseInfo(),
24062208
lvalue.isNontemporal());
@@ -2418,14 +2220,6 @@ mlir::Value CIRGenFunction::buildLoadOfScalar(Address Addr, bool Volatile,
24182220
QualType Ty, SourceLocation Loc,
24192221
LValueBaseInfo BaseInfo,
24202222
bool isNontemporal) {
2421-
return buildLoadOfScalar(Addr, Volatile, Ty, getLoc(Loc), BaseInfo,
2422-
isNontemporal);
2423-
}
2424-
2425-
mlir::Value CIRGenFunction::buildLoadOfScalar(Address Addr, bool Volatile,
2426-
QualType Ty, mlir::Location Loc,
2427-
LValueBaseInfo BaseInfo,
2428-
bool isNontemporal) {
24292223
// TODO(CIR): this has fallen out of sync with codegen
24302224

24312225
// Atomic operations have to be done on integral types
@@ -2435,14 +2229,15 @@ mlir::Value CIRGenFunction::buildLoadOfScalar(Address Addr, bool Volatile,
24352229
}
24362230

24372231
mlir::cir::LoadOp Load = builder.create<mlir::cir::LoadOp>(
2438-
Loc, Addr.getElementType(), Addr.getPointer());
2232+
getLoc(Loc), Addr.getElementType(), Addr.getPointer());
24392233

24402234
if (isNontemporal) {
24412235
llvm_unreachable("NYI");
24422236
}
2443-
2444-
assert(!UnimplementedFeature::tbaa() && "NYI");
2445-
assert(!UnimplementedFeature::emitScalarRangeCheck() && "NYI");
2237+
2238+
// TODO: TBAA
2239+
2240+
// TODO: buildScalarRangeCheck
24462241

24472242
return buildFromMemory(Load, Ty);
24482243
}

0 commit comments

Comments
 (0)