clang 23.0.0git
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"
20#include "clang/AST/Mangle.h"
21#include "clang/Basic/Module.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
32using namespace mlir;
33using namespace cir;
34
35namespace mlir {
36#define GEN_PASS_DEF_IDIOMRECOGNIZER
37#include "clang/CIR/Dialect/Passes.h.inc"
38} // namespace mlir
39
40namespace {
41
42struct 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
58void IdiomRecognizerPass::recognizeStandardLibraryCall(CallOp call) {
59 // To be implemented
60}
61
62void 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
87std::unique_ptr<Pass> mlir::createIdiomRecognizerPass() {
88 return std::make_unique<IdiomRecognizerPass>();
89}
90
91std::unique_ptr<Pass>
93 auto pass = std::make_unique<IdiomRecognizerPass>();
94 pass->setASTContext(astCtx);
95 return std::move(pass);
96}
Defines the clang::ASTContext interface.
Defines the clang::Module class, which describes a module in the source code.
__device__ __2f16 float c
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
std::unique_ptr< Pass > createIdiomRecognizerPass()