clang
23.0.0git
lib
CIR
Dialect
Transforms
IdiomRecognizer.cpp
Go to the documentation of this file.
1
//===- IdiomRecognizer.cpp - recognizing and raising idioms to CIR --------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This pass is responsible for recognizing idioms (such as uses of functions
10
// and types to the C/C++ standard library) and replacing them with Clang IR
11
// operators for later optimization.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "
PassDetail.h
"
16
#include "mlir/Dialect/Func/IR/FuncOps.h"
17
#include "mlir/IR/BuiltinAttributes.h"
18
#include "mlir/IR/Region.h"
19
#include "
clang/AST/ASTContext.h
"
20
#include "
clang/AST/Mangle.h
"
21
#include "
clang/Basic/Module.h
"
22
#include "
clang/CIR/Dialect/Builder/CIRBaseBuilder.h
"
23
#include "
clang/CIR/Dialect/IR/CIRDialect.h
"
24
#include "
clang/CIR/Dialect/Passes.h
"
25
#include "llvm/ADT/SmallVector.h"
26
#include "llvm/ADT/StringMap.h"
27
#include "llvm/ADT/StringRef.h"
28
#include "llvm/ADT/Twine.h"
29
#include "llvm/Support/ErrorHandling.h"
30
#include "llvm/Support/Path.h"
31
32
using namespace
mlir
;
33
using namespace
cir
;
34
35
namespace
mlir
{
36
#define GEN_PASS_DEF_IDIOMRECOGNIZER
37
#include "clang/CIR/Dialect/Passes.h.inc"
38
}
// namespace mlir
39
40
namespace
{
41
42
struct
IdiomRecognizerPass
43
:
public
impl::IdiomRecognizerBase<IdiomRecognizerPass> {
44
IdiomRecognizerPass() =
default
;
45
46
void
runOnOperation()
override
;
47
48
void
recognizeStandardLibraryCall(CallOp call);
49
50
clang::ASTContext *astCtx;
51
void
setASTContext(clang::ASTContext *
c
) { astCtx =
c
; }
52
53
/// Tracks current module.
54
ModuleOp theModule;
55
};
56
}
// namespace
57
58
void
IdiomRecognizerPass::recognizeStandardLibraryCall(CallOp call) {
59
// To be implemented
60
}
61
62
void
IdiomRecognizerPass::runOnOperation() {
63
// The AST context will be used to provide additional information such as
64
// namespaces and template parameter lists that are lost after lowering to
65
// CIR. This information is necessary to recognize many idioms, such as calls
66
// to standard library functions.
67
68
// For now, the AST will be required to allow for faster prototyping and
69
// exploring of new optimizations. In the future, it may be preferable to
70
// make it optional to reduce memory pressure and allow this pass to run
71
// on standalone CIR assembly (Possibly generated from non-Clang front ends).
72
73
assert(astCtx &&
"Missing ASTContext, please construct with the right ctor"
);
74
theModule = getOperation();
75
76
// Process call operations
77
theModule->walk([&](CallOp callOp) {
78
// Skip indirect calls.
79
std::optional<llvm::StringRef> callee = callOp.getCallee();
80
if
(!callee)
81
return
;
82
83
recognizeStandardLibraryCall(callOp);
84
});
85
}
86
87
std::unique_ptr<Pass>
mlir::createIdiomRecognizerPass
() {
88
return
std::make_unique<IdiomRecognizerPass>();
89
}
90
91
std::unique_ptr<Pass>
92
mlir::createIdiomRecognizerPass
(
clang::ASTContext
*astCtx) {
93
auto
pass = std::make_unique<IdiomRecognizerPass>();
94
pass->setASTContext(astCtx);
95
return
std::move(pass);
96
}
ASTContext.h
Defines the clang::ASTContext interface.
CIRBaseBuilder.h
CIRDialect.h
Passes.h
Mangle.h
Module.h
Defines the clang::Module class, which describes a module in the source code.
PassDetail.h
c
__device__ __2f16 float c
Definition
__clang_hip_libdevice_declares.h:296
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition
ASTContext.h:226
cir
Definition
ABIArgInfo.h:22
mlir
Definition
CIRGenerator.h:33
mlir::createIdiomRecognizerPass
std::unique_ptr< Pass > createIdiomRecognizerPass()
Definition
IdiomRecognizer.cpp:87
Generated on
for clang by
1.14.0