Loop tiling (or loop blocking) is a compiler optimization that partitions loop iterations into smaller blocks, or tiles, to fit the working data set into the processor's cache hierarchy. This transformation improves cache locality by ensuring data loaded into fast cache memory is reused multiple times within a tile before being evicted, dramatically reducing costly accesses to main memory (RAM). It is a critical technique for optimizing the performance of matrix multiplication (GEMM) and convolutional layers in neural networks.
Glossary
Loop Tiling

What is Loop Tiling?
Loop tiling is a fundamental compiler optimization for improving cache performance in compute-intensive workloads, particularly in deep learning and scientific computing.
The effectiveness of tiling depends on selecting optimal tile sizes that align with hardware constraints like cache capacity and memory bandwidth. In on-device inference optimization, tiling is often combined with other techniques like operator fusion and vectorization to maximize throughput on edge hardware. Modern deep learning compilers, such as TVM and MLIR, automatically apply tiling as part of their compute graph lowering process to generate efficient code for CPUs, GPUs, and NPUs.
Key Characteristics of Loop Tiling
Loop tiling is a fundamental transformation that restructures nested loops to exploit data locality, primarily targeting the memory hierarchy to reduce cache misses and improve performance.
Core Objective: Cache Locality
The primary goal of loop tiling is to improve cache locality. By partitioning loop iterations into smaller blocks (tiles), the working data set for the inner loops is constrained to a size that fits within the processor's L1 or L2 cache. This maximizes data reuse from the fast cache memory before it is evicted, drastically reducing costly accesses to main memory (RAM).
- Temporal Locality: The same data element is accessed multiple times within a tile.
- Spatial Locality: Data elements stored close together in memory are accessed within the same tile.
Tile Size Selection
Selecting the optimal tile size is critical and involves a trade-off. The tile must be small enough to fit the working set in cache but large enough to amortize the overhead of the tiling transformation.
Key factors include:
- Cache Capacity: The total size of the data accessed within a tile must be less than the target cache's capacity.
- Cache Associativity: To avoid cache conflict misses, tile dimensions are often chosen to be coprime with cache set sizes.
- Hardware Prefetchers: Tile strides should align with hardware prefetching patterns.
Tile sizes are often determined empirically via auto-tuning or through analytical models of the memory hierarchy.
Transformation of Loop Nest
Loop tiling transforms a perfect nest of loops by adding new, outer tiling loops that step through the iteration space in blocks, and inner element loops that iterate within a block.
Original 2D Loop (Matrix Multiplication):
for i in 0..N; for j in 0..N; for k in 0..N; C[i,j] += A[i,k] * B[k,j]
Tiled Version (T_i, T_j, T_k tile sizes):
for ii in 0..N step T_i; for jj in 0..N step T_j; for kk in 0..N step T_k;
for i in ii..min(ii+T_i, N); for j in jj..min(jj+T_j, N); for k in kk..min(kk+T_k, N);
C[i,j] += A[i,k] * B[k,j]
This restructuring changes the order of memory accesses to be more contiguous and reusable.
Interaction with Parallelization
Loop tiling is often a prerequisite for effective parallelization, especially for multi-core CPUs and GPUs. Tiling creates independent units of work that can be distributed across parallel execution units.
- Coarse-Grained Parallelism: Outer tiling loops (
ii,jj) can be parallelized using OpenMP or GPU thread blocks, with each thread or block processing an entire tile. - Fine-Grained Parallelism: Inner element loops within a tile can be vectorized using SIMD instructions (e.g., AVX-512) or mapped to GPU threads.
- Data Race Prevention: Tiling can help isolate data accessed by different parallel workers, reducing false sharing and synchronization overhead.
Application in Deep Learning
Loop tiling is a foundational optimization within deep learning compilers (like TVM, MLIR, XLA) and kernel libraries for accelerating linear algebra operations, which form the core of neural networks.
- Convolution Kernels: Im2col and direct convolution algorithms use tiling to block the input, kernel, and output tensors to fit in cache.
- General Matrix Multiply (GEMM): The performance of libraries like BLIS, Intel oneDNN, and NVIDIA cuBLAS relies heavily on meticulously tuned tiling strategies for multiple levels of cache (L1, L2, L3, TLB).
- Attention Mechanisms: Optimized algorithms like FlashAttention explicitly use tiling to partition the attention computation across the sequence length dimension, enabling processing of very long sequences that exceed GPU SRAM capacity.
Related Compiler Optimizations
Loop tiling is frequently combined with other loop transformations to form a powerful optimization pipeline:
- Loop Interchange: Reorders loop nests to place the loop with the most contiguous memory access pattern innermost, often performed before or after tiling.
- Loop Fusion: Combines multiple adjacent loops, increasing the computational intensity within a single tile.
- Loop Unrolling: Unrolls the innermost loop after tiling to increase instruction-level parallelism and reduce loop overhead.
- Array Padding: Adds unused elements to array dimensions in memory to align data structures with cache line boundaries and prevent conflict misses within tiles.
These transformations are often applied automatically by polyhedral compilers (e.g., Pluto, LLVM Polly) which use a mathematical model to reason about optimal loop nest transformations.
Loop Tiling vs. Related Optimizations
A comparison of loop tiling with other compiler and runtime optimizations used to improve the performance of machine learning workloads, particularly for on-device inference.
| Optimization | Loop Tiling (Blocking) | Operator Fusion | Constant Folding | Just-In-Time (JIT) Compilation |
|---|---|---|---|---|
Primary Goal | Improve cache locality and data reuse | Reduce kernel launch overhead and memory traffic | Simplify compute graph by pre-evaluating constants | Generate hardware-specific kernels at runtime |
Granularity of Application | Loop nest level within an operation | Across sequential operators/layers in a graph | Within individual operators or subgraphs | Entire model compute graph or subgraphs |
Key Mechanism | Partitions loop iterations into smaller blocks (tiles) | Merges multiple ops into a single compiled kernel | Evaluates constant expressions during compilation | Compiles an intermediate representation (IR) to machine code |
Memory Hierarchy Target | CPU Cache (L1/L2/L3) | GPU/NPU Global Memory & Registers | N/A (Compute reduction) | Varies (Can target specific CPU/GPU features) |
Typical Performance Gain | 2x-10x (Reduced cache misses) | 1.2x-3x (Reduced kernel overhead) | < 5% (Minor overhead reduction) | 1.5x-5x (vs. interpreter-based execution) |
Requires Runtime Analysis | Yes (Tile size selection often requires heuristics/profiling) | No (Determined at graph compile time) | No (Purely static analysis) | Yes (Compilation occurs at runtime, may use profile data) |
Hardware Specificity | Moderate (Optimal tile size depends on cache sizes) | High (Fused kernel must be hand-written/auto-tuned per hardware) | Low (General compiler pass) | Very High (Generates code for the exact executing CPU/GPU) |
Interaction with Other Ops | Foundational; enables efficient GEMM/Convolution kernels | Dependent; fuses ops that are already adjacent | Independent; can be applied before or after other passes | Encompassing; can apply tiling/fusion as part of its optimization passes |
Real-World Applications of Loop Tiling
Loop tiling is a foundational compiler optimization critical for performance on edge hardware. Its primary applications focus on maximizing data reuse within fast cache memory to overcome the memory wall.
Image & Signal Processing Pipelines
Real-time video processing on edge devices (e.g., drones, smartphones) relies on tiled loops for filters, transforms, and codecs.
- Applications: JPEG/HEVC encoding, image filtering (blur, edge detection), and FFT (Fast Fourier Transform).
- Process: A frame is processed tile-by-tile (e.g., 64x64 pixel blocks). This keeps working data in cache, allows for parallel processing of independent tiles, and minimizes power-hungry off-chip memory transfers.
- Benefit: Enables real-time 4K video processing on low-power System-on-Chip (SoC) devices with constrained memory subsystems.
Hardware-Specific Tuning for NPUs/GPUs
The effectiveness of tiling depends entirely on the target hardware's memory hierarchy. Optimal tile sizes are co-designed with the hardware.
- GPUs: Tiling is used to optimize access to shared memory (user-managed cache) and register files. CUDA kernels for GEMM explicitly load tiles from global memory to shared memory.
- Neural Processing Units (NPUs): NPU compilers perform aggressive tiling to map tensor computations onto specialized scratchpad memory (SRAM) and minimize DDR traffic. The tile size is a critical parameter for the memory scheduler.
- Constraint: Tile size must be chosen based on cache size, memory bandwidth, and parallel execution units to avoid resource contention and bank conflicts.
Frequently Asked Questions
Loop tiling is a fundamental compiler optimization for high-performance computing and on-device inference. These questions address its core mechanisms, benefits, and practical applications in machine learning.
Loop tiling (also called loop blocking) is a compiler optimization technique that partitions the iteration space of nested loops into smaller blocks, or tiles, to improve cache locality and performance. It works by restructuring loop nests so that data accessed within a tile fits entirely within the processor's fast cache memory, minimizing expensive transfers to and from slower main memory (RAM). The transformation involves splitting large, multi-dimensional loops into an outer loop that steps through tiles and an inner loop that iterates within a tile, ensuring data reused across multiple inner-loop iterations is retained in cache.
For example, a matrix multiplication loop for i, for j, for k can be tiled on the i and j dimensions, creating blocks of the input matrices that are small enough to be loaded into cache and reused for multiple computations before being evicted.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Loop tiling is a foundational compiler optimization that interacts with several other critical techniques for maximizing performance on edge hardware. The following terms are essential for understanding the broader landscape of efficient model execution.
Cache Locality
Cache locality is the overarching optimization principle that loop tiling directly addresses. It refers to designing algorithms and data access patterns to maximize the reuse of data stored in the processor's fast, but limited, cache memory. By keeping frequently accessed data in cache, the system avoids costly trips to main memory (RAM).
- Temporal Locality: Reusing the same data within a short time period.
- Spatial Locality: Accessing data elements that are stored close together in memory. Loop tiling improves both by breaking large data sets into smaller blocks that fit entirely within the cache.
GEMM Optimization
General Matrix Multiply (GEMM) is the core computational kernel for deep learning, and its optimization is paramount. Loop tiling is a fundamental technique within GEMM optimization. The goal is to structure the nested loops of matrix multiplication to maximize data reuse from cache and registers.
- Tiling for Cache: Outer loops are tiled to load sub-matrices (tiles) into the L2/L3 cache.
- Register Tiling: Inner loops are further tiled to keep micro-tiles in CPU registers for the highest speed.
- Vectorization: Within the innermost loop, Single Instruction Multiple Data (SIMD) instructions process multiple data points simultaneously. Effective tiling creates the regular, contiguous access patterns necessary for auto-vectorization.
Operator Fusion
Operator fusion is a complementary compiler optimization that merges multiple sequential neural network operations into a single computational kernel. While loop tiling optimizes within an operation, fusion optimizes between operations.
- Reduces Memory Traffic: Fusing a Convolution, Batch Normalization, and ReLU activation into one kernel avoids writing intermediate tensors to memory and reading them back.
- Synergy with Tiling: A fused operator creates a larger, more complex loop nest. The compiler can then apply loop tiling to this fused kernel, optimizing data movement for the combined computation. This layered approach is key to frameworks like TensorRT and TVM.
Kernel Optimization
Kernel optimization involves hand-writing or auto-generating highly tuned, low-level code for fundamental operations (like GEMM or convolution) on specific hardware. Loop tiling is a manual strategy within kernel optimization.
- Hardware-Specific Tiling: The optimal tile sizes are determined by hardware constants: cache line size, cache capacity (L1, L2, L3), and register file size.
- Beyond Tiling: Expert-optimized kernels also employ techniques like software prefetching to hide memory latency, loop unrolling to reduce branch overhead, and assembly-level instruction scheduling to keep execution units saturated. Libraries like oneDNN and cuBLAS provide such pre-optimized kernels.
Memory Footprint
Memory footprint is the total amount of system memory (RAM) required to load and execute a model. While loop tiling primarily targets cache, it indirectly influences the working memory footprint during inference.
- Peak Activation Memory: For a given layer, the size of the input, output, and any intermediate tensors determines peak memory usage. Tiling can reduce the size of these intermediate buffers by processing data in smaller chunks.
- Interaction with Model Compression: Techniques like quantization and pruning drastically reduce the parameter memory footprint. Loop tiling then optimizes the computation on these already-compressed models, ensuring efficient use of the memory bandwidth and cache hierarchy.
Ahead-Of-Time (AOT) Compilation
Ahead-Of-Time (AOT) Compilation is a deployment paradigm where a model's compute graph is fully optimized and compiled into a standalone executable for a target hardware platform before deployment. Loop tiling is a critical optimization applied during AOT compilation.
- Static Optimization: The compiler (e.g., Apache TVM, MLIR-based compilers) analyzes the model graph, applies loop tiling with hardware-aware tile sizes, and generates fused, tiled kernels.
- Eliminates Runtime Overhead: This pre-compilation removes just-in-time (JIT) compilation latency, making it ideal for resource-constrained edge devices. The deployed binary contains the tiled, memory-optimal version of the model ready for execution.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us