[llvm-dev] [RFC] : LLVM IR should allow bitcast between address spaces with the same size

Serge Pavlov via llvm-dev llvm-dev at lists.llvm.org
Thu Nov 25 10:10:37 PST 2021


There are targets, where address spaces are separate, that is the same
value addresses different entities in different spaces. Size of the
pointers in these spaces can be the same. Cast between such pointers is
invalid operation.

Thanks,
--Serge


On Thu, Nov 25, 2021 at 5:50 PM Sankisa, Krishna (Chaitanya) via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> [AMD Official Use Only]
>
> TL;DR
> =====
>
> We propose the following change to LLVM IR:
> - Allow bitcast to support no-op pointer cast between pointers from
> different address spaces.
> - This bitcast is valid if the bit widths queried for addressspaces from
> datalayout match.
> - Overload CastIsValid call with datalayout argument to check validity of
> cast.
> - Update CastIsValid to allow bitcast between vector of pointers from
> different address spaces if total bit widths match.
> - GVN pass introduces ptrtoint/inttoptr for load which reinterprets bits
> from previous store.
>   Instead use a no-op bitcast of ptrs from different address spaces.
>
>
> Motivation
> ==========
>
> When addrspacecast was introduced, abilty to do no-op pointer bitcast from
> different address spaces has been removed.
> Pointer sizes are always known from DataLayout which is now made mandatory
> in LLVM IR.
> So, Bitcast can be analysed to be no-op cast by matching the pointer sizes
> from DataLayout.
>
> Since there is no other way to do no-op reinterpret of bits, in some cases
> GVN pass introduces a ptrtoint/inttoptr pair.
> After proper analysis, that a no-op bitcast can be done is concluded, then
> a bitcast can be introduced.
> Usage of no-op pointer bitcast between addrspaces can be restricted to be
> used only by IR Transform passes but not by frontend.
>
> For example consider the below IR:
> GVN pass has discovered a reinterpretation of bits via a store followed by
> a load.
>
> %struct.S.coerce = type { i32 addrspace(1)* }
> %s.sroa.0 = alloca i32*, align 8, addrspace(5)
> %0 = extractvalue %struct.S.coerce %s.coerce, 0
> %1 = bitcast i32* addrspace(5)* %s.sroa.0 to i32 addrspace(1)*
> addrspace(5)*
> %2 = addrspacecast i32 addrspace(1)* addrspace(5)* %1 to i32 addrspace(1)
> store i32 addrspace(1)* %0, i32 addrspace(1) %2, align 8
>
> %3 = load i32*, i32* addrspace(5)* %s.sroa.0, align 8, !tbaa !2
>
> ;GVN pass currently introduces no-op ptrotoint/inttoptr for load.
> %3 = ptrtoint i32 addrspace(1)* %0 to i64
> %4 = inttoptr i64 %3 to i32*
>
> ;If bitcast of pointers from different address is allowed, load can be
> replaced with no-op bitcast
> %3 = bitcast i32 addrspace(1)* %0 to i32*
>
>
> Implementation
> ==============
>
> 1. There are certain cases where standalone instructions are created
> without linking them to basicblock/function or module.
>    In such cases DataLayout is not accessible by querying the module. To
> check validity of bitcast datalayout is mandatory.
>    So CastInst::CastIsValid, CastInst::create etc have been overloaded to
> take DataLayout as argument.
>
>    static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy,
>                            const DataLayout &DL);
>
>    static CastInst *Create(
>       Instruction::CastOps,   ///< The opcode of the cast instruction
>       Value *S,               ///< The value to be casted (operand 0)
>       Type *Ty,               ///< The type to which cast should be made
>       const DataLayout &DL,   ///< DataLayout to check validity of bitcast
>       const Twine &Name = "", ///< Name for the instruction
>       Instruction *InsertBefore = nullptr ///< Place to insert the
> instruction
>    );
>
> 2. Verifier has been updated to check for validity of bitcast using
> datalayout.
>
> 3. GVN pass has been updated to use bitcast for a load instead of emitting
> ptrtoint/ inttoptr.
>    llvm/lib/Transforms/Utils/VNCoercion.cpp
>
> Review link: https://reviews.llvm.org/D114533
> ⚙ D114533 LLVM IR should allow bitcast between address spaces with the
> same size. <https://reviews.llvm.org/D114533>
> LLVM IR should allow bitcast between address spaces with the same size.
> reviews.llvm.org
>
> Regards,
> Chaitanya
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20211126/de432fae/attachment.html>


More information about the llvm-dev mailing list