Skip to content

Commit bf5770a

Browse files
committed
Simplify logic further
1 parent a5ab682 commit bf5770a

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

mlir/include/mlir/Target/LLVMIR/LLVMImportInterface.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ class LLVMImportDialectInterface
7474
/// returns the list of supported intrinsic identifiers.
7575
virtual ArrayRef<unsigned> getSupportedIntrinsics() const { return {}; }
7676

77-
/// Whether dialect have a generic way to represent unsupported intrinsics
78-
/// (i.e. as oposed to supported ones aboves).
79-
virtual bool getUnregisteredIntrinsics() const { return false; }
80-
8177
/// Hook for derived dialect interfaces to publish the supported instructions.
8278
/// As every LLVM IR instruction has a unique integer identifier, the function
8379
/// returns the list of supported instruction identifiers. These identifiers
@@ -102,6 +98,12 @@ class LLVMImportDialectInterface
10298
/// efficient dispatch.
10399
class LLVMImportInterface
104100
: public DialectInterfaceCollection<LLVMImportDialectInterface> {
101+
private:
102+
// Generate llvm.call_intrinsic when no supporting dialect available.
103+
LogicalResult
104+
convertUnregisteredIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
105+
LLVM::ModuleImport &moduleImport) const;
106+
105107
public:
106108
using Base::Base;
107109

@@ -149,12 +151,6 @@ class LLVMImportInterface
149151
// Add a mapping for all supported metadata kinds.
150152
for (unsigned kind : iface.getSupportedMetadata(llvmContext))
151153
metadataToDialect[kind].push_back(iface.getDialect());
152-
153-
// There can be only one dialect dealing with unregistered
154-
// intrinsics, the last one to support the interface is the
155-
// one to be used.
156-
if (iface.getUnregisteredIntrinsics())
157-
unregisteredIntrinscToDialect = iface.getDialect();
158154
}
159155

160156
return success();
@@ -176,10 +172,7 @@ class LLVMImportInterface
176172
// No specialized (supported) intrinsics, attempt to generate a generic
177173
// version via llvm.call_intrinsic (if available).
178174
if (!dialect)
179-
dialect = unregisteredIntrinscToDialect;
180-
181-
if (!dialect)
182-
return failure();
175+
return convertUnregisteredIntrinsic(builder, inst, moduleImport);
183176

184177
// Dispatch the conversion to the dialect interface.
185178
const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
@@ -249,9 +242,6 @@ class LLVMImportInterface
249242
DenseMap<unsigned, Dialect *> intrinsicToDialect;
250243
DenseMap<unsigned, const LLVMImportDialectInterface *> instructionToDialect;
251244
DenseMap<unsigned, SmallVector<Dialect *, 1>> metadataToDialect;
252-
253-
/// Unregistered generic and target independent intrinsics.
254-
Dialect *unregisteredIntrinscToDialect = nullptr;
255245
};
256246

257247
} // namespace mlir

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ static ArrayRef<unsigned> getSupportedIntrinsicsImpl() {
5656
return convertibleIntrinsics;
5757
}
5858

59+
/// Converts the LLVM intrinsic to an MLIR LLVM dialect operation if a
60+
/// conversion exits. Returns failure otherwise.
61+
static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
62+
llvm::CallInst *inst,
63+
LLVM::ModuleImport &moduleImport) {
64+
llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
65+
66+
// Check if the intrinsic is convertible to an MLIR dialect counterpart and
67+
// copy the arguments to an an LLVM operands array reference for conversion.
68+
if (isConvertibleIntrinsic(intrinsicID)) {
69+
SmallVector<llvm::Value *> args(inst->args());
70+
ArrayRef<llvm::Value *> llvmOperands(args);
71+
72+
SmallVector<llvm::OperandBundleUse> llvmOpBundles;
73+
llvmOpBundles.reserve(inst->getNumOperandBundles());
74+
for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
75+
llvmOpBundles.push_back(inst->getOperandBundleAt(i));
76+
77+
#include "mlir/Dialect/LLVMIR/LLVMIntrinsicFromLLVMIRConversions.inc"
78+
}
79+
80+
return failure();
81+
}
82+
5983
/// Converts the LLVM intrinsic to a generic LLVM intrinsic call using
6084
/// llvm.intrinsic_call. Returns failure otherwise.
6185
static LogicalResult
@@ -98,32 +122,6 @@ convertUnregisteredIntrinsicImpl(OpBuilder &odsBuilder, llvm::CallInst *inst,
98122
return success();
99123
}
100124

101-
/// Converts the LLVM intrinsic to an MLIR LLVM dialect operation if a
102-
/// conversion exits. Returns failure otherwise.
103-
static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
104-
llvm::CallInst *inst,
105-
LLVM::ModuleImport &moduleImport) {
106-
llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID();
107-
108-
// Check if the intrinsic is convertible to an MLIR dialect counterpart and
109-
// copy the arguments to an an LLVM operands array reference for conversion.
110-
if (isConvertibleIntrinsic(intrinsicID)) {
111-
SmallVector<llvm::Value *> args(inst->args());
112-
ArrayRef<llvm::Value *> llvmOperands(args);
113-
114-
SmallVector<llvm::OperandBundleUse> llvmOpBundles;
115-
llvmOpBundles.reserve(inst->getNumOperandBundles());
116-
for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i)
117-
llvmOpBundles.push_back(inst->getOperandBundleAt(i));
118-
119-
#include "mlir/Dialect/LLVMIR/LLVMIntrinsicFromLLVMIRConversions.inc"
120-
} else if (intrinsicID != llvm::Intrinsic::not_intrinsic) {
121-
return convertUnregisteredIntrinsicImpl(odsBuilder, inst, moduleImport);
122-
}
123-
124-
return failure();
125-
}
126-
127125
/// Returns the list of LLVM IR metadata kinds that are convertible to MLIR LLVM
128126
/// dialect attributes.
129127
static ArrayRef<unsigned> getSupportedMetadataImpl(llvm::LLVMContext &context) {
@@ -411,6 +409,14 @@ static LogicalResult setIntelReqdSubGroupSizeAttr(Builder &builder,
411409
return success();
412410
}
413411

412+
namespace mlir {
413+
// Generate llvm.call_intrinsic when no supporting dialect available.
414+
LogicalResult LLVMImportInterface::convertUnregisteredIntrinsic(
415+
OpBuilder &builder, llvm::CallInst *inst,
416+
LLVM::ModuleImport &moduleImport) const {
417+
return convertUnregisteredIntrinsicImpl(builder, inst, moduleImport);
418+
}
419+
} // namespace mlir
414420
namespace {
415421

416422
/// Implementation of the dialect interface that converts operations belonging
@@ -466,9 +472,6 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
466472
return getSupportedIntrinsicsImpl();
467473
}
468474

469-
/// Cnvertible to generic llvm.call_intrinsic.
470-
bool getUnregisteredIntrinsics() const final { return true; }
471-
472475
/// Returns the list of LLVM IR metadata kinds that are convertible to MLIR
473476
/// LLVM dialect attributes.
474477
ArrayRef<unsigned>

0 commit comments

Comments
 (0)