Skip to content

Commit a023597

Browse files
bcardosolopesxlauko
authored andcommitted
[CIR] Add cir.std.find operation
This is going to be used to raise `cir.call`s to `std::find(...)` into `cir.std.find`.
1 parent a5796b3 commit a023597

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIRDialect.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace impl {
4141
// corresponding trait classes. This avoids them being template
4242
// instantiated/duplicated.
4343
LogicalResult verifySameFirstOperandAndResultType(Operation *op);
44+
LogicalResult verifySameFirstSecondOperandAndResultType(Operation *op);
4445
} // namespace impl
4546

4647
/// This class provides verification for ops that are known to have the same
@@ -55,6 +56,18 @@ class SameFirstOperandAndResultType
5556
}
5657
};
5758

59+
/// This class provides verification for ops that are known to have the same
60+
/// first operand and result type.
61+
///
62+
template <typename ConcreteType>
63+
class SameFirstSecondOperandAndResultType
64+
: public TraitBase<ConcreteType, SameFirstSecondOperandAndResultType> {
65+
public:
66+
static LogicalResult verifyTrait(Operation *op) {
67+
return impl::verifySameFirstSecondOperandAndResultType(op);
68+
}
69+
};
70+
5871
} // namespace OpTrait
5972

6073
namespace cir {

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,46 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
22382238
let hasVerifier = 0;
22392239
}
22402240

2241+
//===----------------------------------------------------------------------===//
2242+
// StdFindOp
2243+
//===----------------------------------------------------------------------===//
2244+
2245+
def SameFirstSecondOperandAndResultType :
2246+
NativeOpTrait<"SameFirstSecondOperandAndResultType">;
2247+
2248+
def StdFindOp : CIR_Op<"std.find", [SameFirstSecondOperandAndResultType]> {
2249+
let arguments = (ins FlatSymbolRefAttr:$original_fn,
2250+
AnyType:$first,
2251+
AnyType:$last,
2252+
AnyType:$pattern);
2253+
let summary = "std:find()";
2254+
let results = (outs AnyType:$result);
2255+
2256+
let description = [{
2257+
Search for `pattern` in data range from `first` to `last`. This currently
2258+
maps to only one form of `std::find`. The `original_fn` operand tracks the
2259+
mangled named that can be used when lowering to a `cir.call`.
2260+
2261+
Example:
2262+
2263+
```mlir
2264+
...
2265+
%result = cir.std.find(@original_fn,
2266+
%first : !T, %last : !T, %pattern : !P) -> !T
2267+
```
2268+
}];
2269+
2270+
let assemblyFormat = [{
2271+
`(`
2272+
$original_fn
2273+
`,` $first `:` type($first)
2274+
`,` $last `:` type($last)
2275+
`,` $pattern `:` type($pattern)
2276+
`)` `->` type($result) attr-dict
2277+
}];
2278+
let hasVerifier = 0;
2279+
}
2280+
22412281
//===----------------------------------------------------------------------===//
22422282
// FAbsOp
22432283
//===----------------------------------------------------------------------===//

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,20 @@ mlir::OpTrait::impl::verifySameFirstOperandAndResultType(Operation *op) {
21632163
return success();
21642164
}
21652165

2166+
LogicalResult
2167+
mlir::OpTrait::impl::verifySameFirstSecondOperandAndResultType(Operation *op) {
2168+
if (failed(verifyAtLeastNOperands(op, 3)) || failed(verifyOneResult(op)))
2169+
return failure();
2170+
2171+
auto checkType = op->getResult(0).getType();
2172+
if (checkType != op->getOperand(0).getType() &&
2173+
checkType != op->getOperand(1).getType())
2174+
return op->emitOpError()
2175+
<< "requires the same type for first operand and result";
2176+
2177+
return success();
2178+
}
2179+
21662180
//===----------------------------------------------------------------------===//
21672181
// CIR attributes
21682182
// FIXME: move all of these to CIRAttrs.cpp

0 commit comments

Comments
 (0)