ClangIR is an ongoing effort to build a high-level intermediate representation(IR) for C/C++ within the LLVM ecosystem. Its key advantage lies in its abilityto retain more source code information. While ClangIR is making progress, itstill lacks certain features, notably ABI handling. Currently, ClangIR lowersmost functions without accounting for ABI-specific calling convention details.GoalsThe “Build & Run SingleSource Benchmarks with ClangIR - Part 2” Google Summer ofCode 2024 builds on my contributions from GSoC 2023 by addressing one of themain issues I encountered: target-specific lowering. It focuses on extendingClangIR’s code generation capabilities, particularly in ABI-lowering for X86-64.Several tests rely on operations and types (e.g., va_arg calls and complexdata types) that require target-specific information to compile correctly.The concrete steps to achieve this were:Implement foundational infrastructure that can scale to multiplearchitectures while adhering to ClangIR design principles such as CodeGenparity, feature guarding, and AST backreferences.Handle basic calling convention scenarios as a proof of concept tovalidate the foundational infrastructure.Add lowering for a second architecture to further validate theinfrastructure’s extensibility to multiple architectures.Unify target-specific ClangIR lowering into the library, as there are afew isolated methods handling target-specific code lowering likecir.va_arg.Integrate calling convention lowering into the main pipeline to ensurefuture contributions and continued development of this infrastructure.ContributionsThe list of contribution (PRs) can be foundhere.Target Lowering LibraryThe most significant contribution of this project was the development of amodular TargetLowering library.This ensures that target-specific MLIR lowering passes can leverage this sharedlibrary for lowering logic. The library also follows ClangIR’s feature guardingprinciples, ensuring that any contributor can refer to the original CodeGen forcontributions, and any unimplemented feature is asserted at specific codepoints, making it easy to track missing functionality.Calling Convention Lowering PassAs a proof of concept, the initial development of the TargetLowering libraryfocused on implementing a calling convention loweringpass that targets multiplearchitectures. Currently, ClangIR ignores the target ABI during CodeGen toretain high-level information. For example, structs are not unraveled to improveargument-passing efficiency. ABI-specific LLVM attributes are also ignored. Thispass addresses these issues by properly tagging LLVM attributes and rewritingfunction definitions and calls to handle unraveled structs. This was implementedfor both X86-64 and AArch64,demonstrating the library’s multi-architecture support.ShortcomingsTarget-Specific Lowering UnificationWhile some target-specific lowering code was moved into the library, it wascopied and pasted rather than properly integrated. This is not ideal forleveraging the library’s multi-architecture features.Inclusion in the Main PipelineThis is still a work in progress, as the library is not yet mature enough tohandle most pre-existing ClangIR tests. There are also feature guards withunreachable statements for many unimplemented features.Future WorkNow that there is a base infrastructure for handling target-agnostic totarget-specific CIR code, there is a large amount of future work to be done,including:Improving DataLayout-related queries using MLIR’s built-in tools.Implementing calling convention lowering for additional types, such aspointers.Extending the TargetLowering library to support more architectures.Unifying remaining target-specific lowering code from other parts of ClangIR.AcknowledgementsI would like to thank my Google Summer of Code mentors, Bruno Cardoso Lopes andNathan Lanza, for another great GSoC experience. I also want to thank the LLVMcommunity and Google for organizing the program.