Files
llvm/offload/unittests/OffloadAPI/program/olIsValidBinary.cpp
Joseph Huber 51e3c3d51b [Offload] Implement 'olIsValidBinary' in offload and clean up (#159658)
Summary:
This exposes the 'isDeviceCompatible' routine for checking if a binary
*can* be loaded. This is useful if people don't want to consume errors
everywhere when figuring out which image to put to what device.

I don't know if this is a good name, I was thining like `olIsCompatible`
or whatever. Let me know what you think.

Long term I'd like to be able to do something similar to what OpenMP
does where we can conditionally only initialize devices if we need them.
That's going to be support needed if we want this to be more
generic.
2025-09-19 12:15:57 -05:00

50 lines
1.6 KiB
C++

//===------- Offload API tests - olIsValidBinary --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "../common/Fixtures.hpp"
#include <OffloadAPI.h>
#include <gtest/gtest.h>
using olIsValidBinaryTest = OffloadDeviceTest;
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olIsValidBinaryTest);
TEST_P(olIsValidBinaryTest, Success) {
std::unique_ptr<llvm::MemoryBuffer> DeviceBin;
ASSERT_TRUE(TestEnvironment::loadDeviceBinary("foo", Device, DeviceBin));
ASSERT_GE(DeviceBin->getBufferSize(), 0lu);
bool IsValid = false;
ASSERT_SUCCESS(olIsValidBinary(Device, DeviceBin->getBufferStart(),
DeviceBin->getBufferSize(), &IsValid));
ASSERT_TRUE(IsValid);
ASSERT_SUCCESS(
olIsValidBinary(Device, DeviceBin->getBufferStart(), 0, &IsValid));
ASSERT_FALSE(IsValid);
}
TEST_P(olIsValidBinaryTest, Invalid) {
std::unique_ptr<llvm::MemoryBuffer> DeviceBin;
ASSERT_TRUE(TestEnvironment::loadDeviceBinary("foo", Device, DeviceBin));
ASSERT_GE(DeviceBin->getBufferSize(), 0lu);
bool IsValid = false;
ASSERT_SUCCESS(
olIsValidBinary(Device, DeviceBin->getBufferStart(), 0, &IsValid));
ASSERT_FALSE(IsValid);
}
TEST_P(olIsValidBinaryTest, NullPointer) {
bool IsValid = false;
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
olIsValidBinary(Device, nullptr, 42, &IsValid));
ASSERT_FALSE(IsValid);
}