﻿# CUDA restrict pointers: an aliasing promise, not a runtime safeguard

Check overlapping tensor views before applying CUDA restrict assumptions, and measure the register-pressure tradeoff only after correctness.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cuda-restrict-pointer-overlapping-views/
Language: en

## Start from accessed memory, not object identity

A CUDA extension can receive different tensor objects whose views overlap in storage. If its output writes into memory that an input still needs to read, compiler assumptions about independent pointers become consequential. The [PyTorch tensor-view documentation](https://docs.pytorch.org/docs/2.15/tensor_view.html) explains why separate objects do not imply separate allocations. Define the kernel's supported overlap behavior before annotating its pointer arguments or adding fast paths for reused output buffers.

## Understand the promise to the compiler

CUDA's [language extensions guide](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-extensions.html) describes __restrict__ as an access promise that enables optimizations. In a writing kernel, a false independence assumption can let the compiler reuse a loaded value or reorder accesses when the program actually needs an intervening write to affect a later read. The annotation does not inspect pointer ranges, create a temporary copy, or protect against overlap. It changes the assumptions under which generated code is valid.

## Trace slices through the whole wrapper

Consider a transform whose output buffer is a shifted slice of its input allocation. Each individual pointer can be valid and every access can remain inside allocated memory, yet the input and output access ranges overlap. Bounds tests alone cannot validate the restrict contract. Either reject unsupported overlap, allocate an independent destination, or use an implementation whose documented semantics handle the permitted pattern. Do not remove the annotation and assume that cross-thread ordering problems have thereby been solved.

## Include build flags in the review

The [NVCC options guide](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/nvcc.html) lists the compiler's restrict option as an assertion about kernel pointer parameters. A build flag can therefore introduce a broad access assumption even when a source-level signature looks unannotated. Record the relevant compilation settings alongside the wrapper's ownership rules. Review caller-owned buffers and tensor views when moving a kernel into a shared extension build, where previously local flags may now affect additional entry points.

## Measure only valid alternatives

Restricted pointers can reduce repeated work, but the language guide also discusses increased register pressure and possible occupancy costs. An optimization is not established merely by adding the keyword. Compare only implementations that satisfy the same access contract, then examine generated resource use and timings. Existing guides on [register spills](/journal/learn-register-spill-local-memory/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) provide context. Keep the primary conclusion bounded: verified non-overlapping access permits an assumption; it does not guarantee a speedup.
