[BitcodeReader] Allow reading pointer types from old IR
authorSebastian Neubauer <Sebastian.Neubauer@amd.com>
Wed, 18 Jan 2023 12:19:59 +0000 (13:19 +0100)
committerSebastian Neubauer <Sebastian.Neubauer@amd.com>
Wed, 18 Jan 2023 12:20:15 +0000 (13:20 +0100)
commitc33b9395b1dfe9f8506062c05c658f6541f6163d
tree983297dfb4e3ea52b0f3b5005dc0f3f42d05df1f
parentf615de7e266813518237b7d5b24ac53d1eec6b3c
[BitcodeReader] Allow reading pointer types from old IR

When opaque pointers are enabled and old IR with typed pointers is read,
the BitcodeReader automatically upgrades all typed pointers to opaque
pointers. This is a lossy conversion, i.e. when a function argument is a
pointer and unused, it’s impossible to reconstruct the original type
behind the pointer.

There are cases where the type information of pointers is needed. One is
reading DXIL, which is bitcode of old LLVM IR and makes a lot of use of
pointers in function signatures.
We’d like to keep using up-to-date llvm to read in and process DXIL, so
in the face of opaque pointers, we need some way to access the type
information of pointers from the read bitcode.

This patch allows extracting type information by supplying functions to
parseBitcodeFile that get called for each function signature or metadata
value. The function can access the type information via the reader’s
type IDs and the getTypeByID and getContainedTypeID functions.
The tests exemplarily shows how type info from pointers can be stored in
metadata for use after the BitcodeReader finished.

Differential Revision: https://reviews.llvm.org/D127728
llvm/include/llvm/Bitcode/BitcodeReader.h
llvm/include/llvm/IRReader/IRReader.h
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/lib/Bitcode/Reader/MetadataLoader.cpp
llvm/lib/Bitcode/Reader/MetadataLoader.h
llvm/lib/IRReader/IRReader.cpp
llvm/tools/llc/llc.cpp
llvm/tools/opt/opt.cpp
llvm/unittests/Bitcode/BitReaderTest.cpp
llvm/unittests/Bitcode/BitReaderTestCode.h [new file with mode: 0644]