https://github.com/HendrikHuebner updated https://github.com/llvm/llvm-project/pull/172487
From 7770875219acdad8990eb84b2681fd0d36fc7ee2 Mon Sep 17 00:00:00 2001 From: hhuebner <[email protected]> Date: Tue, 16 Dec 2025 15:50:20 +0100 Subject: [PATCH 1/2] libopt --- clang/include/clang/CIR/CIRToCIRPasses.h | 3 +- clang/include/clang/CIR/Dialect/Passes.h | 2 + clang/include/clang/CIR/Dialect/Passes.td | 19 +++ .../include/clang/Frontend/FrontendOptions.h | 3 + clang/include/clang/Options/Options.td | 9 +- .../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 + clang/lib/CIR/Dialect/Transforms/LibOpt.cpp | 108 ++++++++++++++++++ clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 5 +- clang/lib/CIR/Lowering/CIRPasses.cpp | 16 ++- clang/test/CIR/Transforms/lib-opt.cpp | 3 + 10 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 clang/lib/CIR/Dialect/Transforms/LibOpt.cpp create mode 100644 clang/test/CIR/Transforms/lib-opt.cpp diff --git a/clang/include/clang/CIR/CIRToCIRPasses.h b/clang/include/clang/CIR/CIRToCIRPasses.h index 4a23790ee8b76..ed82fe4477e82 100644 --- a/clang/include/clang/CIR/CIRToCIRPasses.h +++ b/clang/include/clang/CIR/CIRToCIRPasses.h @@ -33,7 +33,8 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, clang::ASTContext &astCtx, bool enableVerifier, - bool enableCIRSimplify); + bool enableCIRSimplify, + llvm::StringRef libOptOptions); } // namespace cir diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h index 32c3e27d07dfb..dbd6fb198b845 100644 --- a/clang/include/clang/CIR/Dialect/Passes.h +++ b/clang/include/clang/CIR/Dialect/Passes.h @@ -27,6 +27,8 @@ std::unique_ptr<Pass> createHoistAllocasPass(); std::unique_ptr<Pass> createLoweringPreparePass(); std::unique_ptr<Pass> createLoweringPreparePass(clang::ASTContext *astCtx); std::unique_ptr<Pass> createGotoSolverPass(); +std::unique_ptr<Pass> createLibOptPass(); +std::unique_ptr<Pass> createLibOptPass(clang::ASTContext *astCtx); void populateCIRPreLoweringPasses(mlir::OpPassManager &pm); diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td index 0f5783945f8ae..043292377bbed 100644 --- a/clang/include/clang/CIR/Dialect/Passes.td +++ b/clang/include/clang/CIR/Dialect/Passes.td @@ -93,4 +93,23 @@ def LoweringPrepare : Pass<"cir-lowering-prepare"> { let dependentDialects = ["cir::CIRDialect"]; } +def LibOpt : Pass<"cir-lib-opt"> { + let summary = "Optimize C/C++ library calls"; + let description = [{ + By using higher level information from `cir-idiom-recognize`, this pass + apply transformations to CIR based on specific C/C++ library semantics. + + Transformations done by this pass can be inspected by users by using + remarks. Currently supported are `all` and `transforms`. + }]; + let constructor = "mlir::createLibOptPass()"; + let dependentDialects = ["cir::CIRDialect"]; + + let options = [ + ListOption<"remarksList", "remarks", "std::string", + "Diagnostic remarks to enable" + " Supported styles: {all|transforms}", "llvm::cl::ZeroOrMore">, + ]; +} + #endif // CLANG_CIR_DIALECT_PASSES_TD diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index ba7da56cb9fce..6cee55f09c47d 100644 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -422,6 +422,9 @@ class FrontendOptions { LLVM_PREFERRED_TYPE(bool) unsigned ClangIRDisableCIRVerifier : 1; + // Options to control ClangIR library optimization + std::string clangIRLibOptOptions; + CodeCompleteOptions CodeCompleteOpts; /// Specifies the output format of the AST. diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 756d6deed7130..c4230a9c3bab4 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -3181,7 +3181,14 @@ def clangir_disable_verifier : Flag<["-"], "clangir-disable-verifier">, Visibility<[ClangOption, CC1Option]>, HelpText<"ClangIR: Disable MLIR module verifier">, MarshallingInfoFlag<FrontendOpts<"ClangIRDisableCIRVerifier">>; - +def fclangir_lib_opt_EQ : Joined<["-"], "fclangir-lib-opt=">, + Visibility<[ClangOption, CC1Option]>, Group<f_Group>, + HelpText<"Enable C/C++ library based optimizations (with options)">, + MarshallingInfoString<FrontendOpts<"clangIRLibOptOptions">>; +def fclangir_lib_opt : Flag<["-"], "fclangir-lib-opt">, + Visibility<[ClangOption, CC1Option]>, Group<f_Group>, + Alias<fclangir_lib_opt_EQ>, AliasArgs<[""]>, + HelpText<"Enable C/C++ library based optimizations">; defm clangir : BoolFOption<"clangir", FrontendOpts<"UseClangIRPipeline">, DefaultFalse, PosFlag<SetTrue, [], [ClangOption, CC1Option], "Use the ClangIR pipeline to compile">, diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt index 3fc5b06b74e4d..b11695172d12e 100644 --- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt +++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt @@ -6,6 +6,7 @@ add_clang_library(MLIRCIRTransforms LoweringPrepare.cpp LoweringPrepareItaniumCXXABI.cpp GotoSolver.cpp + LibOpt.cpp DEPENDS MLIRCIRPassIncGen diff --git a/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp new file mode 100644 index 0000000000000..6b75d961ce4b0 --- /dev/null +++ b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp @@ -0,0 +1,108 @@ +//===- LibOpt.cpp - Optimize CIR raised C/C++ library idioms --------------===// +// +// 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 pass optimizes C/C++ standard library idioms in Clang IR. +// +//===----------------------------------------------------------------------===// + +#include "PassDetail.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/Region.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Mangle.h" +#include "clang/Basic/Module.h" +#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" +#include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "clang/CIR/Dialect/Passes.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Path.h" + +using cir::CIRBaseBuilderTy; +using namespace mlir; +using namespace cir; + +namespace mlir { +#define GEN_PASS_DEF_LIBOPT +#include "clang/CIR/Dialect/Passes.h.inc" +} // namespace mlir + +namespace { + +struct LibOptPass : public impl::LibOptBase<LibOptPass> { + LibOptPass() = default; + void runOnOperation() override; + + class PassOptions { + enum : unsigned { + None = 0, + RemarkTransforms = 1, + RemarkAll = 1 << 1, + }; + + public: + void parseOption(const llvm::StringRef remark) { + value |= StringSwitch<unsigned>(remark) + .Case("transforms", RemarkTransforms) + .Case("all", RemarkAll) + .Default(None); + } + + void parseOptions(LibOptPass &pass) { + if (isOptionsParsed) + return; + + for (const llvm::StringRef &remark : pass.remarksList) + parseOption(remark); + + isOptionsParsed = true; + } + + bool emitRemarkAll() { return value & RemarkAll; } + bool emitRemarkTransforms() { + return emitRemarkAll() || value & RemarkTransforms; + } + + private: + unsigned value = None; + bool isOptionsParsed = false; + }; + + PassOptions passOptions; + + /// + /// AST related + /// ----------- + clang::ASTContext *astCtx; + void setASTContext(clang::ASTContext *c) { astCtx = c; } + + /// Tracks current module. + ModuleOp theModule; +}; +} // namespace + +void LibOptPass::runOnOperation() { + passOptions.parseOptions(*this); + auto *op = getOperation(); + if (isa<::mlir::ModuleOp>(op)) + theModule = cast<::mlir::ModuleOp>(op); +} + +std::unique_ptr<Pass> mlir::createLibOptPass() { + return std::make_unique<LibOptPass>(); +} + +std::unique_ptr<Pass> mlir::createLibOptPass(clang::ASTContext *astCtx) { + auto pass = std::make_unique<LibOptPass>(); + pass->setASTContext(astCtx); + return std::move(pass); +} diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index 67bb5657d4001..de4e3b176edfe 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -108,11 +108,14 @@ class CIRGenConsumer : public clang::ASTConsumer { mlir::ModuleOp MlirModule = Gen->getModule(); mlir::MLIRContext &MlirCtx = Gen->getMLIRContext(); + if (!FEOptions.ClangIRDisablePasses) { + std::string libOptOptions = FEOptions.clangIRLibOptOptions; + // Setup and run CIR pipeline. if (runCIRToCIRPasses(MlirModule, MlirCtx, C, !FEOptions.ClangIRDisableCIRVerifier, - CGO.OptimizationLevel > 0) + CGO.OptimizationLevel > 0, libOptOptions) .failed()) { CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed); return; diff --git a/clang/lib/CIR/Lowering/CIRPasses.cpp b/clang/lib/CIR/Lowering/CIRPasses.cpp index ccc838717e421..9b292fb4502b1 100644 --- a/clang/lib/CIR/Lowering/CIRPasses.cpp +++ b/clang/lib/CIR/Lowering/CIRPasses.cpp @@ -21,16 +21,30 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, clang::ASTContext &astContext, bool enableVerifier, - bool enableCIRSimplify) { + bool enableCIRSimplify, + llvm::StringRef libOptOptions) { llvm::TimeTraceScope scope("CIR To CIR Passes"); + // TODO(CIR): Make this actually propagate errors correctly. This is stubbed + // in to get rebases going. + + auto errorHandler = [](const llvm::Twine &) -> mlir::LogicalResult { + return mlir::LogicalResult::failure(); + }; + mlir::PassManager pm(&mlirContext); pm.addPass(mlir::createCIRCanonicalizePass()); if (enableCIRSimplify) pm.addPass(mlir::createCIRSimplifyPass()); + auto libOpPass = mlir::createLibOptPass(); + if (libOpPass->initializeOptions(libOptOptions, errorHandler).failed()) { + return mlir::failure(); + } + + pm.addPass(std::move(libOpPass)); pm.addPass(mlir::createLoweringPreparePass(&astContext)); pm.enableVerifier(enableVerifier); diff --git a/clang/test/CIR/Transforms/lib-opt.cpp b/clang/test/CIR/Transforms/lib-opt.cpp new file mode 100644 index 0000000000000..ea4c265a1a584 --- /dev/null +++ b/clang/test/CIR/Transforms/lib-opt.cpp @@ -0,0 +1,3 @@ +// RUN: %clang_cc1 -fclangir -emit-cir -mmlir --mlir-print-ir-after-all %s -o - 2>&1 | FileCheck %s -check-prefix=CIR + +// CIR: IR Dump After LibOpt (cir-lib-opt) From c53b71065948b39aca995624e22bf217bb33ac57 Mon Sep 17 00:00:00 2001 From: hhuebner <[email protected]> Date: Fri, 19 Dec 2025 18:11:38 +0100 Subject: [PATCH 2/2] formatting --- clang/include/clang/CIR/CIRToCIRPasses.h | 10 ++++------ clang/include/clang/CIR/Dialect/Passes.td | 6 ++++-- clang/lib/CIR/Dialect/Transforms/LibOpt.cpp | 2 +- clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 1 - clang/lib/CIR/Lowering/CIRPasses.cpp | 10 ++++------ 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/clang/include/clang/CIR/CIRToCIRPasses.h b/clang/include/clang/CIR/CIRToCIRPasses.h index ed82fe4477e82..9927a7a5ae726 100644 --- a/clang/include/clang/CIR/CIRToCIRPasses.h +++ b/clang/include/clang/CIR/CIRToCIRPasses.h @@ -29,12 +29,10 @@ class ModuleOp; namespace cir { // Run set of cleanup/prepare/etc passes CIR <-> CIR. -mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, - mlir::MLIRContext &mlirCtx, - clang::ASTContext &astCtx, - bool enableVerifier, - bool enableCIRSimplify, - llvm::StringRef libOptOptions); +mlir::LogicalResult +runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, + clang::ASTContext &astCtx, bool enableVerifier, + bool enableCIRSimplify, llvm::StringRef libOptOptions); } // namespace cir diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td index 043292377bbed..0dc40ad988c46 100644 --- a/clang/include/clang/CIR/Dialect/Passes.td +++ b/clang/include/clang/CIR/Dialect/Passes.td @@ -96,12 +96,14 @@ def LoweringPrepare : Pass<"cir-lowering-prepare"> { def LibOpt : Pass<"cir-lib-opt"> { let summary = "Optimize C/C++ library calls"; let description = [{ - By using higher level information from `cir-idiom-recognize`, this pass - apply transformations to CIR based on specific C/C++ library semantics. + This pass applies transformations on C/C++ standard library idioms, + such as library function calls and structs raised to CIR operations + using the `cir-idiom-recognize` pass. Transformations done by this pass can be inspected by users by using remarks. Currently supported are `all` and `transforms`. }]; + let constructor = "mlir::createLibOptPass()"; let dependentDialects = ["cir::CIRDialect"]; diff --git a/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp index 6b75d961ce4b0..8f4e80fba33d6 100644 --- a/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp +++ b/clang/lib/CIR/Dialect/Transforms/LibOpt.cpp @@ -61,7 +61,7 @@ struct LibOptPass : public impl::LibOptBase<LibOptPass> { if (isOptionsParsed) return; - for (const llvm::StringRef &remark : pass.remarksList) + for (const llvm::StringRef remark : pass.remarksList) parseOption(remark); isOptionsParsed = true; diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp index de4e3b176edfe..3c9ba24331232 100644 --- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -108,7 +108,6 @@ class CIRGenConsumer : public clang::ASTConsumer { mlir::ModuleOp MlirModule = Gen->getModule(); mlir::MLIRContext &MlirCtx = Gen->getMLIRContext(); - if (!FEOptions.ClangIRDisablePasses) { std::string libOptOptions = FEOptions.clangIRLibOptOptions; diff --git a/clang/lib/CIR/Lowering/CIRPasses.cpp b/clang/lib/CIR/Lowering/CIRPasses.cpp index 9b292fb4502b1..16e7e028f50b3 100644 --- a/clang/lib/CIR/Lowering/CIRPasses.cpp +++ b/clang/lib/CIR/Lowering/CIRPasses.cpp @@ -17,12 +17,10 @@ #include "llvm/Support/TimeProfiler.h" namespace cir { -mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, - mlir::MLIRContext &mlirContext, - clang::ASTContext &astContext, - bool enableVerifier, - bool enableCIRSimplify, - llvm::StringRef libOptOptions) { +mlir::LogicalResult +runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, + clang::ASTContext &astContext, bool enableVerifier, + bool enableCIRSimplify, llvm::StringRef libOptOptions) { llvm::TimeTraceScope scope("CIR To CIR Passes"); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
