forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from Xilinx/matthias.pdl_builtin
PDLL: Allow to define builtin native calls
- Loading branch information
Showing
11 changed files
with
277 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===- Builtins.h - Builtin functions of the PDL dialect --------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines builtin functions of the PDL dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_PDL_IR_BUILTINS_H_ | ||
#define MLIR_DIALECT_PDL_IR_BUILTINS_H_ | ||
|
||
namespace mlir { | ||
class PDLPatternModule; | ||
class Attribute; | ||
class PatternRewriter; | ||
|
||
namespace pdl { | ||
void registerBuiltins(PDLPatternModule &pdlPattern); | ||
|
||
namespace builtin { | ||
Attribute createDictionaryAttr(PatternRewriter &rewriter); | ||
Attribute addEntryToDictionaryAttr(PatternRewriter &rewriter, | ||
Attribute dictAttr, Attribute attrName, | ||
Attribute attrEntry); | ||
Attribute createArrayAttr(PatternRewriter &rewriter); | ||
Attribute addElemToArrayAttr(PatternRewriter &rewriter, Attribute attr, | ||
Attribute element); | ||
} // namespace builtin | ||
} // namespace pdl | ||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_PDL_IR_BUILTINS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <mlir/Dialect/PDL/IR/Builtins.h> | ||
#include <mlir/IR/PatternMatch.h> | ||
|
||
using namespace mlir; | ||
|
||
namespace mlir::pdl { | ||
namespace builtin { | ||
mlir::Attribute createDictionaryAttr(mlir::PatternRewriter &rewriter) { | ||
return rewriter.getDictionaryAttr({}); | ||
} | ||
|
||
mlir::Attribute addEntryToDictionaryAttr(mlir::PatternRewriter &rewriter, | ||
mlir::Attribute dictAttr, | ||
mlir::Attribute attrName, | ||
mlir::Attribute attrEntry) { | ||
assert(isa<DictionaryAttr>(dictAttr)); | ||
auto attr = dictAttr.cast<DictionaryAttr>(); | ||
auto name = attrName.cast<StringAttr>(); | ||
std::vector<NamedAttribute> values = attr.getValue().vec(); | ||
|
||
// Remove entry if it exists in the dictionary. | ||
llvm::erase_if(values, [&](NamedAttribute &namedAttr) { | ||
return namedAttr.getName() == name.getValue(); | ||
}); | ||
|
||
values.push_back(rewriter.getNamedAttr(name, attrEntry)); | ||
return rewriter.getDictionaryAttr(values); | ||
} | ||
|
||
mlir::Attribute createArrayAttr(mlir::PatternRewriter &rewriter) { | ||
return rewriter.getArrayAttr({}); | ||
} | ||
|
||
mlir::Attribute addElemToArrayAttr(mlir::PatternRewriter &rewriter, | ||
mlir::Attribute attr, | ||
mlir::Attribute element) { | ||
assert(isa<ArrayAttr>(attr)); | ||
auto values = cast<ArrayAttr>(attr).getValue().vec(); | ||
values.push_back(element); | ||
return rewriter.getArrayAttr(values); | ||
} | ||
} // namespace builtin | ||
|
||
void registerBuiltins(PDLPatternModule &pdlPattern) { | ||
using namespace builtin; | ||
// See Parser::defineBuiltins() | ||
pdlPattern.registerRewriteFunction("__builtin_createDictionaryAttr", | ||
createDictionaryAttr); | ||
pdlPattern.registerRewriteFunction("__builtin_addEntryToDictionaryAttr", | ||
addEntryToDictionaryAttr); | ||
pdlPattern.registerRewriteFunction("__builtin_createArrayAttr", | ||
createArrayAttr); | ||
pdlPattern.registerRewriteFunction("__builtin_addElemToArrayAttr", | ||
addElemToArrayAttr); | ||
} | ||
} // namespace mlir::pdl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_mlir_dialect_library(MLIRPDLDialect | ||
Builtins.cpp | ||
PDL.cpp | ||
PDLTypes.cpp | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.