Posts

Showing posts from April, 2025

Project Stage III: Multi-Clone Analysis in GCC Pass

 Introduction In Stage III, I enhanced my GCC pass to analyze programs with multiple cloned functions simultaneously, making individual PRUNE/NOPRUNE decisions for each clone. This involved refining GIMPLE code comparisons, handling compiler optimization challenges, and validating results on both x86_64 and aarch64 architectures. Implementation Core Logic (Key Code Snippets) 1. Data Structure for Clones // Stores GIMPLE statements and operand types for each function struct GimpleStmtInfo { int code; std::vector<tree> operands; }; std::map<std::string, std::vector<GimpleStmtInfo>> function_map; 2. Clone Detection // Identify cloned functions by name (e.g., "func.clone1") if (func_name.find(base_name + ".") == 0 && func_name.find(".resolver") == std::string::npos) { // Compare GIMPLE sequences bool is_same = (base_gimple == cloned_gimple); fprintf(dump_file, "Result: %s\n", is...

Project Stage 2: Clone-Prune Analysis Pass Implementation

Introduction This project implements a GCC compiler pass to analyze cloned functions created during Function Multi-Versioning (FMV). The goal is to determine if clones are "substantially identical" to enable pruning. Below is my detailed implementation journey. Phase 1: Clone Detection & Retrieval Data Structure for Tracking Clones std::map<std::string, std::vector<std::string>> clone_map; // Key: Base name (e.g., "scale_samples") // Value: Variants (e.g., {"scale_samples.avx2", "scale_samples.sse4"}) Function Retrieval Logic void locate_clones(const std::map<std::string, std::vector<std::string>>& clone_map) { for (const auto& [base, variants] : clone_map) { if (variants.size() != 2) { fprintf(dump_file, "Invalid clone count for %s: %zu\n", base.c_str(), variants.size()); continue; } function *clone1 = nullptr, *clone2 = nul...

SPO600 2025 Winter Project: Stage 1 - Creating a Basic GCC Pass

Image
Project Overview In this project, I enhanced the GNU Compiler Collection (GCC) by adding a custom compiler pass to analyze functions during compilation. The pass performs three key tasks: Prints the name of every function being compiled. Counts the number of basic blocks in each function. Counts the number of GIMPLE statements within each basic block. This pass helps developers understand code structure and optimization impacts. Below, I detail my implementation journey, including challenges and solutions. Environment Setup To avoid conflicts with the system compiler, I used three isolated directories: Source Directory : ~/git/gcc (GCC source code). Build Directory : ~/gcc-build-001 (compilation artifacts). Install Directory : ~/gcc-test-001 (custom GCC installation). Why This Matters : Separating source, build, and install directories ensures a clean workflow and simplifies debugging. Step 1: Creating the Custom Pass File : tree_tpatel103_pass.cc I created a new file in gcc/gcc to hou...