@@ -719,6 +719,22 @@ namespace pi2ur {
719
719
720
720
inline pi_result piTearDown (void *PluginParameter) {
721
721
std::ignore = PluginParameter;
722
+ // Fetch the single known adapter (the one which is statically linked) so we
723
+ // can release it. Fetching it for a second time (after piPlatformsGet)
724
+ // increases the reference count, so we need to release it twice.
725
+ // pi_unified_runtime has its own implementation of piTearDown.
726
+ static std::once_flag AdapterReleaseFlag;
727
+ ur_adapter_handle_t Adapter;
728
+ ur_result_t Ret = UR_RESULT_SUCCESS;
729
+ std::call_once (AdapterReleaseFlag, [&]() {
730
+ Ret = urAdapterGet (1 , &Adapter, nullptr );
731
+ if (Ret == UR_RESULT_SUCCESS) {
732
+ Ret = urAdapterRelease (Adapter);
733
+ Ret = urAdapterRelease (Adapter);
734
+ }
735
+ });
736
+ HANDLE_ERRORS (Ret);
737
+
722
738
// TODO: Dont check for errors in urTearDown, since
723
739
// when using Level Zero plugin, the second urTearDown
724
740
// will fail as ur_loader.so has already been unloaded,
@@ -731,9 +747,20 @@ inline pi_result piTearDown(void *PluginParameter) {
731
747
inline pi_result piPlatformsGet (pi_uint32 NumEntries, pi_platform *Platforms,
732
748
pi_uint32 *NumPlatforms) {
733
749
734
- urInit (0 );
750
+ urInit (0 , nullptr );
751
+ // We're not going through the UR loader so we're guaranteed to have exactly
752
+ // one adapter (whichever is statically linked). The PI plugin for UR has its
753
+ // own implementation of piPlatformsGet.
754
+ static ur_adapter_handle_t Adapter;
755
+ static std::once_flag AdapterGetFlag;
756
+ ur_result_t Ret = UR_RESULT_SUCCESS;
757
+ std::call_once (AdapterGetFlag,
758
+ [&Ret]() { Ret = urAdapterGet (1 , &Adapter, nullptr ); });
759
+ HANDLE_ERRORS (Ret);
760
+
735
761
auto phPlatforms = reinterpret_cast <ur_platform_handle_t *>(Platforms);
736
- HANDLE_ERRORS (urPlatformGet (NumEntries, phPlatforms, NumPlatforms));
762
+ HANDLE_ERRORS (
763
+ urPlatformGet (&Adapter, 1 , NumEntries, phPlatforms, NumPlatforms));
737
764
return PI_SUCCESS;
738
765
}
739
766
@@ -894,8 +921,18 @@ inline pi_result piDeviceRelease(pi_device Device) {
894
921
return PI_SUCCESS;
895
922
}
896
923
897
- inline pi_result piPluginGetLastError (char **message) {
898
- std::ignore = message;
924
+ inline pi_result piPluginGetLastError (char **Message) {
925
+ // We're not going through the UR loader so we're guaranteed to have exactly
926
+ // one adapter (whichever is statically linked). The PI plugin for UR has its
927
+ // own implementation of piPluginGetLastError. Materialize the adapter
928
+ // reference for the urAdapterGetLastError call, then release it.
929
+ ur_adapter_handle_t Adapter;
930
+ urAdapterGet (1 , &Adapter, nullptr );
931
+ int32_t ErrorCode;
932
+ urAdapterGetLastError (Adapter, const_cast <const char **>(Message),
933
+ &ErrorCode);
934
+ urAdapterRelease (Adapter);
935
+
899
936
return PI_SUCCESS;
900
937
}
901
938
0 commit comments