Skip to content

Commit 400be51

Browse files
authored
Use IRParser instead of BitcodeReader (#3399)
The IRParser offers a higher level API, that allows us to also read bitcode assembly, and not only compiled bitcode. This allows us to skip the `llvm-as %s -o %t.bc` step in several tests. I've only updated 3 tests to show the difference; but we have more than 500 occurrences of `llvm-as %s -o` in the tests: > $ grep -r "llvm-as %s -o" | cut -d':' -f1 | sort -u | wc -l > 506 As a bonus, the function `llvm::getLazyIRFileModule` (https://llvm.org/doxygen/namespacellvm.html#aac226baa3ffd0f255a8d2b6d978b81b2) also calls `MemoryBuffer::getFileOrSTDIN`.
1 parent 6238ea3 commit 400be51

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

test/addrspacecast_null.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -o %t.spv
1+
; RUN: llvm-spirv %s -o %t.spv
32
; RUN: spirv-dis %t.spv | FileCheck %s
43

54
; Test that addrspacecast of null pointer generates appropriate OpConstantNull

test/align-duplicate.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -o %t.spv
1+
; RUN: llvm-spirv %s -o %t.spv
32
; RUN: spirv-val %t.spv
43

54
; Test that duplicate align information does not result in SPIR-V validation

test/array-alloca.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -o %t.spv
1+
; RUN: llvm-spirv %s -o %t.spv
32

43
; Validation test.
54
; RUN: spirv-val %t.spv
65

76
; SPIR-V codegen test.
8-
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
7+
; RUN: llvm-spirv %s -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
98

109
; Roundtrip test.
1110
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM
1211

13-
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
12+
; RUN: llvm-spirv %s -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
1413
; RUN: spirv-val %t.spv
15-
; RUN: llvm-spirv %t.bc -spirv-text --spirv-ext=+SPV_KHR_untyped_pointers -o - | FileCheck %s --check-prefix=CHECK-SPIRV
14+
; RUN: llvm-spirv %s -spirv-text --spirv-ext=+SPV_KHR_untyped_pointers -o - | FileCheck %s --check-prefix=CHECK-SPIRV
1615
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM
1716

1817
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"

tools/llvm-spirv/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
44
BitReader
55
BitWriter
66
Core
7+
IRReader
78
Passes
89
Support
910
TargetParser

tools/llvm-spirv/llvm-spirv.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
#include "llvm/ADT/APInt.h"
5050
#include "llvm/ADT/SmallVector.h"
5151
#include "llvm/ADT/StringRef.h"
52-
#include "llvm/Bitcode/BitcodeReader.h"
5352
#include "llvm/Bitcode/BitcodeWriter.h"
5453
#include "llvm/IR/LLVMContext.h"
5554
#include "llvm/IR/Module.h"
5655
#include "llvm/IR/Verifier.h"
56+
#include "llvm/IRReader/IRReader.h"
5757
#include "llvm/Support/CommandLine.h"
5858
#include "llvm/Support/Debug.h"
5959
#include "llvm/Support/Error.h"
@@ -62,6 +62,7 @@
6262
#include "llvm/Support/MemoryBuffer.h"
6363
#include "llvm/Support/PrettyStackTrace.h"
6464
#include "llvm/Support/Signals.h"
65+
#include "llvm/Support/SourceMgr.h"
6566
#include "llvm/Support/ToolOutputFile.h"
6667
#include "llvm/Support/raw_ostream.h"
6768

@@ -375,11 +376,14 @@ class StreambufToArray : public std::streambuf {
375376
static int convertLLVMToSPIRV(const SPIRV::TranslatorOpts &Opts) {
376377
LLVMContext Context;
377378

378-
std::unique_ptr<MemoryBuffer> MB =
379-
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFile)));
380-
std::unique_ptr<Module> M =
381-
ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context,
382-
/*ShouldLazyLoadMetadata=*/true));
379+
SMDiagnostic GetIRErr;
380+
std::unique_ptr<Module> M = getLazyIRFileModule(
381+
InputFile, GetIRErr, Context, /*ShouldLazyLoadMetadata=*/true);
382+
if (!M) {
383+
ExitOnErr(
384+
createStringError(inconvertibleErrorCode(), GetIRErr.getMessage()));
385+
}
386+
383387
ExitOnErr(M->materializeAll());
384388

385389
if (OutputFile.empty()) {

0 commit comments

Comments
 (0)