Skip to content
Merged
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
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class Address {
return nullptr;
return getPointer().getDefiningOp();
}

template <typename T> T getDefiningOp() const {
return mlir::dyn_cast_or_null<T>(getDefiningOp());
}
};

} // namespace clang::CIRGen
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void CIRGenFunction::emitAutoVarInit(const AutoVarEmission &emission) {
// out of it while trying to build the expression, mark it as such.
auto addr = lv.getAddress().getPointer();
assert(addr && "Should have an address");
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(addr.getDefiningOp());
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
assert(allocaOp && "Address should come straight out of the alloca");

if (!allocaOp.use_empty())
Expand Down Expand Up @@ -617,7 +617,7 @@ void CIRGenFunction::emitStaticVarDecl(const VarDecl &D,
// TODO(cir): we should have a way to represent global ops as values without
// having to emit a get global op. Sometimes these emissions are not used.
auto addr = getBuilder().createGetGlobal(globalOp);
auto getAddrOp = mlir::cast<cir::GetGlobalOp>(addr.getDefiningOp());
auto getAddrOp = addr.getDefiningOp<cir::GetGlobalOp>();

CharUnits alignment = getContext().getDeclAlign(&D);

Expand Down
24 changes: 10 additions & 14 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,

// Update the alloca with more info on initialization.
assert(addr.getPointer() && "expected pointer to exist");
auto SrcAlloca =
dyn_cast_or_null<cir::AllocaOp>(addr.getPointer().getDefiningOp());
auto SrcAlloca = addr.getDefiningOp<cir::AllocaOp>();
if (currVarDecl && SrcAlloca) {
const VarDecl *VD = currVarDecl;
assert(VD && "VarDecl expected");
Expand Down Expand Up @@ -1328,8 +1327,7 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *E) {
emitPointerWithAlignment(E->getSubExpr(), &BaseInfo, &TBAAInfo);

// Tag 'load' with deref attribute.
if (auto loadOp =
dyn_cast<cir::LoadOp>(Addr.getPointer().getDefiningOp())) {
if (auto loadOp = Addr.getDefiningOp<cir::LoadOp>()) {
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
}

Expand Down Expand Up @@ -2221,8 +2219,7 @@ static Address createReferenceTemporary(CIRGenFunction &CGF,
if (const clang::ValueDecl *extDecl = M->getExtendingDecl()) {
auto extDeclAddrIter = CGF.LocalDeclMap.find(extDecl);
if (extDeclAddrIter != CGF.LocalDeclMap.end()) {
extDeclAlloca = dyn_cast_if_present<cir::AllocaOp>(
extDeclAddrIter->second.getDefiningOp());
extDeclAlloca = extDeclAddrIter->second.getDefiningOp<cir::AllocaOp>();
}
}
mlir::OpBuilder::InsertPoint ip;
Expand Down Expand Up @@ -2337,7 +2334,7 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
Address Alloca = Address::invalid();
Address Object = createReferenceTemporary(*this, M, E, &Alloca);

if (auto Var = dyn_cast<cir::GlobalOp>(Object.getPointer().getDefiningOp())) {
if (auto Var = Object.getDefiningOp<cir::GlobalOp>()) {
// TODO(cir): add something akin to stripPointerCasts() to ptr above
assert(0 && "NYI");
} else {
Expand Down Expand Up @@ -2867,7 +2864,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
addr = builder.createAlloca(loc, /*addr type*/ localVarPtrTy,
/*var type*/ ty, name, alignIntAttr, arraySize);
if (currVarDecl) {
auto alloca = cast<cir::AllocaOp>(addr.getDefiningOp());
auto alloca = addr.getDefiningOp<cir::AllocaOp>();
alloca.setAstAttr(ASTVarDeclAttr::get(&getMLIRContext(), currVarDecl));
}
}
Expand Down Expand Up @@ -3097,9 +3094,9 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
const Twine &Name,
mlir::Value ArraySize,
bool insertIntoFnEntryBlock) {
return cast<cir::AllocaOp>(emitAlloca(Name.str(), Ty, Loc, CharUnits(),
insertIntoFnEntryBlock, ArraySize)
.getDefiningOp());
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), insertIntoFnEntryBlock,
ArraySize)
.getDefiningOp<cir::AllocaOp>();
}

/// This creates an alloca and inserts it into the provided insertion point
Expand All @@ -3109,9 +3106,8 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
mlir::OpBuilder::InsertPoint ip,
mlir::Value ArraySize) {
assert(ip.isSet() && "Insertion point is not set");
return cast<cir::AllocaOp>(
emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
.getDefiningOp());
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
.getDefiningOp<cir::AllocaOp>();
}

/// Just like CreateTempAlloca above, but place the alloca into the function
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// direclty in the parent scope removing the need to hoist it.
assert(retAlloca.getDefiningOp() && "expected a alloca op");
CGF.getBuilder().hoistAllocaToParentRegion(
cast<cir::AllocaOp>(retAlloca.getDefiningOp()));
retAlloca.getDefiningOp<cir::AllocaOp>());

return CGF.emitLoadOfScalar(CGF.makeAddrLValue(retAlloca, E->getType()),
E->getExprLoc());
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mlir::LogicalResult CIRGenFunction::declare(const Decl *var, QualType ty,
assert(!symbolTable.count(var) && "not supposed to be available just yet");

addr = emitAlloca(namedVar->getName(), ty, loc, alignment);
auto allocaOp = cast<cir::AllocaOp>(addr.getDefiningOp());
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
if (isParam)
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
if (ty->isReferenceType() || ty.isConstQualified())
Expand All @@ -313,7 +313,7 @@ mlir::LogicalResult CIRGenFunction::declare(Address addr, const Decl *var,
assert(!symbolTable.count(var) && "not supposed to be available just yet");

addrVal = addr.getPointer();
auto allocaOp = cast<cir::AllocaOp>(addrVal.getDefiningOp());
auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
if (isParam)
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
if (ty->isReferenceType() || ty.isConstQualified())
Expand Down Expand Up @@ -1986,7 +1986,7 @@ void CIRGenFunction::emitVarAnnotations(const VarDecl *decl, mlir::Value val) {
for (const auto *annot : decl->specific_attrs<AnnotateAttr>()) {
annotations.push_back(CGM.emitAnnotateAttr(annot));
}
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(val.getDefiningOp());
auto allocaOp = val.getDefiningOp<cir::AllocaOp>();
assert(allocaOp && "expects available alloca");
allocaOp.setAnnotationsAttr(builder.getArrayAttr(annotations));
}
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2653,10 +2653,10 @@ inline mlir::Value DominatingCIRValue::restore(CIRGenFunction &CGF,
return value.getPointer();

// Otherwise, it should be an alloca instruction, as set up in save().
auto alloca = mlir::cast<cir::AllocaOp>(value.getPointer().getDefiningOp());
auto alloca = value.getPointer().getDefiningOp<cir::AllocaOp>();
mlir::Value val = CGF.getBuilder().createAlignedLoad(
alloca.getLoc(), alloca.getType(), alloca);
cir::LoadOp loadOp = mlir::cast<cir::LoadOp>(val.getDefiningOp());
cir::LoadOp loadOp = val.getDefiningOp<cir::LoadOp>();
loadOp.setAlignment(alloca.getAlignment());
return val;
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ Value tryFoldCastChain(cir::CastOp op) {
if (!isIntOrBoolCast(op))
break;
head = op;
op = dyn_cast_or_null<cir::CastOp>(head.getSrc().getDefiningOp());
op = head.getSrc().getDefiningOp<cir::CastOp>();
}

if (head == tail)
Expand Down Expand Up @@ -841,7 +841,7 @@ static bool isBoolNot(cir::UnaryOp op) {
// and the argument of the first one (%0) will be used instead.
OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
if (isBoolNot(*this))
if (auto previous = dyn_cast_or_null<UnaryOp>(getInput().getDefiningOp()))
if (auto previous = getInput().getDefiningOp<UnaryOp>())
if (isBoolNot(previous))
return previous.getInput();

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/Dialect/Transforms/LibOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ void LibOptPass::xformStdFindIntoMemchr(StdFindOp findOp) {
// Build memchr op:
// void *memchr(const void *s, int c, size_t n);
auto memChr = [&] {
if (auto iterBegin = dyn_cast<IterBeginOp>(first.getDefiningOp());
iterBegin && isa<IterEndOp>(last.getDefiningOp())) {
if (auto iterBegin = first.getDefiningOp<IterBeginOp>();
iterBegin && last.getDefiningOp<IterEndOp>()) {
// Both operands have the same type, use iterBegin.

// Look at this pointer to retrieve container information.
Expand Down
38 changes: 19 additions & 19 deletions clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ static std::string getVarNameFromValue(mlir::Value v) {
if (auto allocaOp = dyn_cast<AllocaOp>(srcOp))
return allocaOp.getName().str();
if (auto getElemOp = dyn_cast<GetMemberOp>(srcOp)) {
auto parent = dyn_cast<AllocaOp>(getElemOp.getAddr().getDefiningOp());
auto parent = getElemOp.getAddr().getDefiningOp<cir::AllocaOp>();
if (parent) {
llvm::SmallString<128> finalName;
llvm::raw_svector_ostream Out(finalName);
Expand Down Expand Up @@ -760,9 +760,9 @@ void LifetimeCheckPass::checkReturn(ReturnOp retOp) {

// The return value is loaded from the return slot before
// returning.
auto loadOp = dyn_cast<LoadOp>(retOp.getOperand(0).getDefiningOp());
auto loadOp = retOp.getOperand(0).getDefiningOp<cir::LoadOp>();
assert(loadOp && "expected cir.load");
if (!isa<AllocaOp>(loadOp.getAddr().getDefiningOp()))
if (!loadOp.getAddr().getDefiningOp<cir::AllocaOp>())
return;

// Keep track of interesting lambda.
Expand Down Expand Up @@ -1083,10 +1083,10 @@ void LifetimeCheckPass::checkCoroTaskStore(StoreOp storeOp) {
// Bind values that are coming from alloca's (like %arg0 above) to the
// pset of %task - this effectively leads to some invalidation of %task
// when %arg0 finishes its lifetime at the end of the enclosing cir.scope.
if (auto call = dyn_cast<cir::CallOp>(taskTmp.getDefiningOp())) {
if (auto call = taskTmp.getDefiningOp<cir::CallOp>()) {
bool potentialTaintedTask = false;
for (auto arg : call.getArgOperands()) {
auto alloca = dyn_cast<cir::AllocaOp>(arg.getDefiningOp());
auto alloca = arg.getDefiningOp<cir::AllocaOp>();
if (alloca && currScope->localValues.count(alloca)) {
getPmap()[taskAddr].insert(State::getLocalValue(alloca));
potentialTaintedTask = true;
Expand All @@ -1103,11 +1103,11 @@ void LifetimeCheckPass::checkCoroTaskStore(StoreOp storeOp) {
}

mlir::Value LifetimeCheckPass::getLambdaFromMemberAccess(mlir::Value addr) {
auto op = addr.getDefiningOp();
// FIXME: we likely want to consider more indirections here...
if (!isa<cir::GetMemberOp>(op))
auto op = addr.getDefiningOp<cir::GetMemberOp>();
if (!op)
return nullptr;
auto allocaOp = dyn_cast<cir::AllocaOp>(op->getOperand(0).getDefiningOp());
auto allocaOp = op->getOperand(0).getDefiningOp<cir::AllocaOp>();
if (!allocaOp || !isLambdaType(allocaOp.getAllocaType()))
return nullptr;
return allocaOp;
Expand All @@ -1117,7 +1117,7 @@ void LifetimeCheckPass::checkLambdaCaptureStore(StoreOp storeOp) {
auto localByRefAddr = storeOp.getValue();
auto lambdaCaptureAddr = storeOp.getAddr();

if (!isa_and_nonnull<cir::AllocaOp>(localByRefAddr.getDefiningOp()))
if (!localByRefAddr.getDefiningOp<cir::AllocaOp>())
return;
auto lambdaAddr = getLambdaFromMemberAccess(lambdaCaptureAddr);
if (!lambdaAddr)
Expand Down Expand Up @@ -1178,7 +1178,7 @@ void LifetimeCheckPass::updatePointsTo(mlir::Value addr, mlir::Value data,
mlir::Location loc) {

auto getArrayFromSubscript = [&](PtrStrideOp strideOp) -> mlir::Value {
auto castOp = dyn_cast<CastOp>(strideOp.getBase().getDefiningOp());
auto castOp = strideOp.getBase().getDefiningOp<cir::CastOp>();
if (!castOp)
return {};
if (castOp.getKind() != cir::CastKind::array_to_ptrdecay)
Expand Down Expand Up @@ -1284,11 +1284,11 @@ void LifetimeCheckPass::checkStore(StoreOp storeOp) {
// Decompose store's to aggregates into multiple updates to individual fields.
if (aggregates.count(addr)) {
auto data = storeOp.getValue();
auto dataSrcOp = data.getDefiningOp();
// Only interested in updating and tracking fields, anything besides
// constants isn't really relevant.
if (dataSrcOp && isa<ConstantOp>(dataSrcOp))
if (data.getDefiningOp<cir::ConstantOp>()) {
// Only interested in updating and tracking fields, anything besides
// constants isn't really relevant.
updatePointsTo(addr, data, data.getLoc());
}
return;
}

Expand Down Expand Up @@ -1343,7 +1343,7 @@ void LifetimeCheckPass::emitInvalidHistory(mlir::InFlightDiagnostic &D,
case InvalidStyle::EndOfScope: {
if (tasks.count(histKey)) {
llvm::StringRef resource = "resource";
if (auto allocaOp = dyn_cast<AllocaOp>(info.val->getDefiningOp())) {
if (auto allocaOp = info.val->getDefiningOp<cir::AllocaOp>()) {
if (isLambdaType(allocaOp.getAllocaType()))
resource = "lambda";
}
Expand Down Expand Up @@ -1419,7 +1419,7 @@ void LifetimeCheckPass::checkPointerDeref(mlir::Value addr, mlir::Location loc,
D << "returned lambda captures local variable";
else if (derefStyle == DerefStyle::CallParam ||
derefStyle == DerefStyle::IndirectCallParam) {
bool isAgg = isa_and_nonnull<GetMemberOp>(addr.getDefiningOp());
bool isAgg = addr.getDefiningOp<cir::GetMemberOp>();
D << "passing ";
if (!isAgg)
D << "invalid pointer";
Expand Down Expand Up @@ -1464,7 +1464,7 @@ mlir::Value LifetimeCheckPass::getThisParamPointerCategory(CallOp callOp) {
auto thisptr = callOp.getArgOperand(0);
if (ptrs.count(thisptr))
return thisptr;
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
if (ptrs.count(loadOp.getAddr()))
return loadOp.getAddr();
}
Expand All @@ -1476,7 +1476,7 @@ mlir::Value LifetimeCheckPass::getThisParamOwnerCategory(CallOp callOp) {
auto thisptr = callOp.getArgOperand(0);
if (owners.count(thisptr))
return thisptr;
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
if (owners.count(loadOp.getAddr()))
return loadOp.getAddr();
}
Expand Down Expand Up @@ -1580,7 +1580,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, cir::CXXCtorAttr ctor) {

// Not interested in block/function arguments or any indirect
// provided alloca address.
if (!dyn_cast_or_null<AllocaOp>(addr.getDefiningOp()))
if (!addr.getDefiningOp<cir::AllocaOp>())
return;

markPsetNull(addr, callOp.getLoc());
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ static std::string getGlobalVarNameForConstString(cir::StoreOp op,
Out << "module.";
}

auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(op.getAddr().getDefiningOp());
auto allocaOp = op.getAddr().getDefiningOp<cir::AllocaOp>();
if (allocaOp && !allocaOp.getName().empty())
Out << allocaOp.getName();
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mlir::Value createCoercedBitcast(mlir::Value Src, mlir::Type DestTy,
LowerFunction &CGF) {
auto destPtrTy = cir::PointerType::get(DestTy);

if (auto load = mlir::dyn_cast<LoadOp>(Src.getDefiningOp()))
if (auto load = Src.getDefiningOp<cir::LoadOp>())
return CGF.getRewriter().create<CastOp>(Src.getLoc(), destPtrTy,
CastKind::bitcast, load.getAddr());

Expand Down Expand Up @@ -89,7 +89,7 @@ mlir::Value enterRecordPointerForCoercedAccess(mlir::Value SrcPtr,
auto ptrTy = PointerType::get(FirstElt);
if (mlir::isa<RecordType>(SrcPtr.getType())) {
auto addr = SrcPtr;
if (auto load = mlir::dyn_cast<LoadOp>(SrcPtr.getDefiningOp()))
if (auto load = SrcPtr.getDefiningOp<cir::LoadOp>())
addr = load.getAddr();
cir_cconv_assert(mlir::isa<PointerType>(addr.getType()));
// we can not use getMemberOp here since we need a pointer to the first
Expand Down Expand Up @@ -369,11 +369,11 @@ mlir::Value emitAddressAtOffset(LowerFunction &LF, mlir::Value addr,
/// a non fundamental integer type
mlir::Value createCoercedNonFundamental(mlir::Value src, mlir::Type ty,
LowerFunction &LF) {
if (auto load = mlir::dyn_cast<LoadOp>(src.getDefiningOp())) {
if (auto load = src.getDefiningOp<cir::LoadOp>()) {
auto &bld = LF.getRewriter();
auto addr = load.getAddr();

auto oldAlloca = mlir::dyn_cast<AllocaOp>(addr.getDefiningOp());
auto oldAlloca = addr.getDefiningOp<cir::AllocaOp>();
auto alloca = bld.create<AllocaOp>(
src.getLoc(), cir::PointerType::get(ty), ty,
/*name=*/llvm::StringRef(""), oldAlloca.getAlignmentAttr());
Expand Down Expand Up @@ -751,7 +751,7 @@ LowerFunction::buildFunctionEpilog(const LowerFunctionInfo &FI) {

auto retInputs = ret.getInput();
assert(retInputs.size() == 1 && "return should only have one input");
if (auto load = mlir::dyn_cast<LoadOp>(retInputs[0].getDefiningOp()))
if (auto load = retInputs[0].getDefiningOp<cir::LoadOp>())
if (load.getResult().use_empty())
rewriter.eraseOp(load);

Expand Down
Loading
Loading