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

Sankisa, Krishna (Chaitanya) via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 29 04:49:49 PST 2021


[AMD Official Use Only]


This bitcast is intended to be used by transform passes when its been decided that the resultant operation is no-op( Like in GVN case of inserting ptrtoint/inttoptr ).
When reinterpretation of bits is not supported by target, the resultant value is poison.

Regards,
Chaitanya.
________________________________
From: Serge Pavlov <sepavloff at gmail.com>
Sent: 25 November 2021 23:40
To: Sankisa, Krishna (Chaitanya) <Krishna.Sankisa at amd.com>
Cc: llvm-dev at lists.llvm.org <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] [RFC] : LLVM IR should allow bitcast between address spaces with the same size

[CAUTION: External Email]
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<mailto: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<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Freviews.llvm.org%2FD114533&data=04%7C01%7CKrishna.Sankisa%40amd.com%7C91a2205fc5cd40f6e48d08d9b03eea61%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637734607278960617%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=E2akTQbj%2FTDNihgs1kJ5bsNE0R6zWg7uOF6%2FFnJ70Jo%3D&reserved=0>
⚙ D114533 LLVM IR should allow bitcast between address spaces with the same size.<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Freviews.llvm.org%2FD114533&data=04%7C01%7CKrishna.Sankisa%40amd.com%7C91a2205fc5cd40f6e48d08d9b03eea61%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637734607278970611%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=DEZgHHVYC2fQTw5VFLB6A9Dx5dz4XmHTHD3lLC%2BPyCQ%3D&reserved=0>
LLVM IR should allow bitcast between address spaces with the same size.
reviews.llvm.org<https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Freviews.llvm.org%2F&data=04%7C01%7CKrishna.Sankisa%40amd.com%7C91a2205fc5cd40f6e48d08d9b03eea61%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637734607278970611%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=OY8kK7T4mg%2BpWRqV%2FMfDsx7vfJ3aW5da0pi4Yt0BeWI%3D&reserved=0>

Regards,
Chaitanya
_______________________________________________
LLVM Developers mailing list
llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fllvm-dev&data=04%7C01%7CKrishna.Sankisa%40amd.com%7C91a2205fc5cd40f6e48d08d9b03eea61%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637734607278980606%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=gbTdDj%2FC9%2Bs7FfHV0cPKLl8Knv%2F9y%2FExlP%2Fquw4Bdgg%3D&reserved=0>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20211129/793d3fec/attachment-0001.html>


More information about the llvm-dev mailing list