microsoft / qsharp-language

Official repository for design of the quantum programming language Q# and its core libraries
MIT License
235 stars 56 forks source link

QIR: Consider avoiding variadic functions in the runtime #37

Closed IrinaYatsenko closed 4 years ago

IrinaYatsenko commented 4 years ago

As far as I can tell, there is no platform-agnostic way to implement variadic functions at the IR level and it's also error-prone, which creates a burden on the runtime implementers.

Instead of requiring:

%Array* __quantum__rt__array_create(i32, i32, ...)
i8* __quantum__rt__array_get_element_ptr(%Array*, ...)

consider similar to 1D specialization for 2D arrays (the arguments will be still passed via registers) and a generic buffer-based signature for larger arrays:

%Array* __quantum__rt__array_create_2d(i32, i64, i64)
i8* __quantum__rt__array_get_element_ptr_2d(%Array*, i64, i64)
%Array* __quantum__rt__array_create(i32, i32 %buffer_length, i64* %buffer)
i8* __quantum__rt__array_get_element_ptr(%Array*, i32 %buffer_length, i64* %buffer)

Notes:

alan-geller commented 4 years ago

LLVM does specify a way to do this using intrinsics, but I agree, it's awkward in C/C++. Unfortunately the i64* approach is also awkward on the LLVM side; I think the idea of adding 2d specialized versions ameliorates that, because I suspect that manipulating arrays of more than 2 dimensions will be rare.

IrinaYatsenko commented 4 years ago

LLVM does specify a way to do this using intrinsics

This is the intrinsics I'm using in the bridge (https://github.com/microsoft/qsharp-runtime/blob/main/src/QirRuntime/lib/QIR/bridge-rt.ll) and am still having issues on Linux. Is it possible that different platforms must set up differently for the call? If so, we still have cross-platform compatibility issue.

alan-geller commented 4 years ago

LLVM does specify a way to do this using intrinsics

This is the intrinsics I'm using in the bridge (https://github.com/microsoft/qsharp-runtime/blob/main/src/QirRuntime/lib/QIR/bridge-rt.ll) and am still having issues on Linux. Is it possible that different platforms must set up differently for the call? If so, we still have cross-platform compatibility issue.

Agreed. In the PR (#55) I went with your suggestion.