[mlir] std::optional::value => operator*/operator->

value() has undesired exception checking semantics and calls
__throw_bad_optional_access in libc++. Moreover, the API is unavailable without
_LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see
_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS).
This commit is contained in:
Fangrui Song 2022-12-17 04:38:27 +00:00
parent 2098ad7f00
commit 4913e5da3c
8 changed files with 28 additions and 30 deletions

View File

@ -687,7 +687,7 @@ TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy,
DenseElementsAttr TensorLiteralParser::getStringAttr(SMLoc loc, ShapedType type,
Type eltTy) {
if (hexStorage.has_value()) {
auto stringValue = hexStorage.value().getStringValue();
auto stringValue = hexStorage->getStringValue();
return DenseStringElementsAttr::get(type, {stringValue});
}

View File

@ -77,7 +77,7 @@ LogicalResult ScalarOpToLibmCall<Op, TypeResolver>::matchAndRewrite(
if (!isDouble.has_value())
return failure();
auto name = isDouble.value() ? doubleFunc : floatFunc;
auto name = *isDouble ? doubleFunc : floatFunc;
auto opFunc = dyn_cast_or_null<SymbolOpInterface>(
SymbolTable::lookupSymbolIn(module, name));

View File

@ -136,12 +136,12 @@ uint64_t mlir::getLargestDivisorOfTripCount(AffineForOp forOp) {
thisGcd = resultExpr.getLargestKnownDivisor();
}
if (gcd.has_value())
gcd = std::gcd(gcd.value(), thisGcd);
gcd = std::gcd(*gcd, thisGcd);
else
gcd = thisGcd;
}
assert(gcd.has_value() && "value expected per above logic");
return gcd.value();
return *gcd;
}
/// Given an induction variable `iv` of type AffineForOp and an access `index`

View File

@ -453,13 +453,13 @@ public:
if (firstSrcDepPos.has_value()) {
if (lastDstDepPos.has_value()) {
if (firstSrcDepPos.value() <= lastDstDepPos.value()) {
if (*firstSrcDepPos <= *lastDstDepPos) {
// No valid insertion point exists which preserves dependences.
return nullptr;
}
}
// Return the insertion point at 'firstSrcDepPos'.
return depInsts[firstSrcDepPos.value()];
return depInsts[*firstSrcDepPos];
}
// No dependence targets in range (or only dst deps in range), return
// 'dstNodInst' insertion point.
@ -973,11 +973,10 @@ static Value createPrivateMemRef(AffineForOp forOp, Operation *srcStoreOpInst,
// Create 'newMemRefType' using 'newShape' from MemRefRegion accessed
// by 'srcStoreOpInst'.
uint64_t bufSize =
getMemRefEltSizeInBytes(oldMemRefType) * numElements.value();
uint64_t bufSize = getMemRefEltSizeInBytes(oldMemRefType) * *numElements;
unsigned newMemSpace;
if (bufSize <= localBufSizeThreshold && fastMemorySpace.has_value()) {
newMemSpace = fastMemorySpace.value();
newMemSpace = *fastMemorySpace;
} else {
newMemSpace = oldMemRefType.getMemorySpaceAsInt();
}
@ -1175,7 +1174,7 @@ static bool isFusionProfitable(Operation *srcOpInst, Operation *srcStoreOpInst,
srcWriteRegion.getRegionSize();
if (!maybeSrcWriteRegionSizeBytes.has_value())
return false;
int64_t srcWriteRegionSizeBytes = maybeSrcWriteRegionSizeBytes.value();
int64_t srcWriteRegionSizeBytes = *maybeSrcWriteRegionSizeBytes;
// Compute op instance count for the src loop nest.
uint64_t dstLoopNestCost = getComputeCost(dstForOp, dstLoopNestStats);
@ -1216,13 +1215,13 @@ static bool isFusionProfitable(Operation *srcOpInst, Operation *srcStoreOpInst,
Optional<int64_t> maybeSliceWriteRegionSizeBytes =
sliceWriteRegion.getRegionSize();
if (!maybeSliceWriteRegionSizeBytes.has_value() ||
maybeSliceWriteRegionSizeBytes.value() == 0) {
*maybeSliceWriteRegionSizeBytes == 0) {
LLVM_DEBUG(llvm::dbgs()
<< "Failed to get slice write region size at loopDepth: " << i
<< "\n");
continue;
}
int64_t sliceWriteRegionSizeBytes = maybeSliceWriteRegionSizeBytes.value();
int64_t sliceWriteRegionSizeBytes = *maybeSliceWriteRegionSizeBytes;
// If we are fusing for reuse, check that write regions remain the same.
// TODO: Write region check should check sizes and offsets in
@ -1299,11 +1298,11 @@ static bool isFusionProfitable(Operation *srcOpInst, Operation *srcStoreOpInst,
return false;
}
auto srcMemSizeVal = srcMemSize.value();
auto dstMemSizeVal = dstMemSize.value();
auto srcMemSizeVal = *srcMemSize;
auto dstMemSizeVal = *dstMemSize;
assert(sliceMemEstimate && "expected value");
auto fusedMem = dstMemSizeVal + sliceMemEstimate.value();
auto fusedMem = dstMemSizeVal + *sliceMemEstimate;
LLVM_DEBUG(llvm::dbgs() << " src mem: " << srcMemSizeVal << "\n"
<< " dst mem: " << dstMemSizeVal << "\n"

View File

@ -242,7 +242,7 @@ StringRef AttrOrTypeParameter::getComparator() const {
StringRef AttrOrTypeParameter::getCppType() const {
if (auto *stringType = dyn_cast<llvm::StringInit>(getDef()))
return stringType->getValue();
return getDefValue<llvm::StringInit>("cppType").value();
return *getDefValue<llvm::StringInit>("cppType");
}
StringRef AttrOrTypeParameter::getCppAccessorType() const {

View File

@ -682,7 +682,7 @@ GlobalOp Importer::processGlobal(llvm::GlobalVariable *globalVar) {
uint64_t alignment = 0;
llvm::MaybeAlign maybeAlign = globalVar->getAlign();
if (maybeAlign.has_value()) {
llvm::Align align = maybeAlign.value();
llvm::Align align = *maybeAlign;
alignment = align.value();
}

View File

@ -386,11 +386,11 @@ static bool inlineHistoryIncludes(
MutableArrayRef<std::pair<CallGraphNode *, std::optional<size_t>>>
inlineHistory) {
while (inlineHistoryID.has_value()) {
assert(inlineHistoryID.value() < inlineHistory.size() &&
assert(*inlineHistoryID < inlineHistory.size() &&
"Invalid inline history ID");
if (inlineHistory[inlineHistoryID.value()].first == node)
if (inlineHistory[*inlineHistoryID].first == node)
return true;
inlineHistoryID = inlineHistory[inlineHistoryID.value()].second;
inlineHistoryID = inlineHistory[*inlineHistoryID].second;
}
return false;
}
@ -543,7 +543,7 @@ static LogicalResult inlineCallsInSCC(Inliner &inliner, CGUseList &useList,
inlineHistory.push_back(std::make_pair(it.targetNode, inlineHistoryID));
auto historyToString = [](InlineHistoryT h) {
return h.has_value() ? std::to_string(h.value()) : "root";
return h.has_value() ? std::to_string(*h) : "root";
};
(void)historyToString;
LLVM_DEBUG(llvm::dbgs()

View File

@ -740,14 +740,14 @@ static LogicalResult generateNamedGenericOpOds(LinalgOpConfig &opConfig,
assert(arg.indexAttrMap.has_value());
assert(arg.defaultIndices.has_value());
size_t size = arg.indexAttrMap->affineMap().getNumResults();
assert(arg.defaultIndices.value().size() == size);
assert(arg.defaultIndices->size() == size);
static const char typeFmt[] = "RankedI64ElementsAttr<[{0}]>";
static const char defFmt[] =
"DefaultValuedOptionalAttr<{0}, \"{ {1} }\">:${2}";
std::string defaultVals;
llvm::raw_string_ostream ss(defaultVals);
llvm::interleave(
arg.defaultIndices.value(), ss,
*arg.defaultIndices, ss,
[&](int64_t val) { ss << "static_cast<int64_t>(" << val << ")"; },
", ");
attrDefs.push_back(llvm::formatv(defFmt, llvm::formatv(typeFmt, size),
@ -1098,11 +1098,10 @@ if ({1}Iter != attrs.end()) {{
if (expression.scalarFn->attrName) {
if (llvm::none_of(args, [&](LinalgOperandDef &arg) {
return isFunctionAttribute(arg.kind) &&
arg.name == expression.scalarFn->attrName.value();
arg.name == *expression.scalarFn->attrName;
})) {
emitError(genContext.getLoc())
<< "missing function attribute "
<< expression.scalarFn->attrName.value();
emitError(genContext.getLoc()) << "missing function attribute "
<< *expression.scalarFn->attrName;
}
funcType = llvm::formatv("{0}Val", *expression.scalarFn->attrName);
}
@ -1113,15 +1112,15 @@ if ({1}Iter != attrs.end()) {{
if (expression.scalarFn->kind == ScalarFnKind::Type) {
assert(expression.scalarFn->typeVar.has_value());
Optional<std::string> typeCppValue =
findTypeValue(expression.scalarFn->typeVar.value(), args);
findTypeValue(*expression.scalarFn->typeVar, args);
if (!typeCppValue) {
emitError(genContext.getLoc())
<< "type variable " << expression.scalarFn->typeVar.value()
<< "type variable " << *expression.scalarFn->typeVar
<< ", used in a type conversion, must map to a predefined or "
<< "an argument type but it does not";
return std::nullopt;
}
operandCppValues.push_back(typeCppValue.value());
operandCppValues.push_back(*typeCppValue);
}
// Collect the scalar operands.