@@ -259,7 +259,8 @@ static pi_result mapError(ze_result_t ZeResult) {
259
259
{ZE_RESULT_ERROR_INVALID_NATIVE_BINARY, PI_INVALID_BINARY},
260
260
{ZE_RESULT_ERROR_INVALID_KERNEL_NAME, PI_INVALID_KERNEL_NAME},
261
261
{ZE_RESULT_ERROR_INVALID_FUNCTION_NAME, PI_BUILD_PROGRAM_FAILURE},
262
- {ZE_RESULT_ERROR_OVERLAPPING_REGIONS, PI_INVALID_OPERATION}};
262
+ {ZE_RESULT_ERROR_OVERLAPPING_REGIONS, PI_INVALID_OPERATION},
263
+ {ZE_RESULT_ERROR_MODULE_BUILD_FAILURE, PI_BUILD_PROGRAM_FAILURE}};
263
264
auto It = ErrorMapping.find (ZeResult);
264
265
if (It == ErrorMapping.end ()) {
265
266
return PI_ERROR_UNKNOWN;
@@ -1634,22 +1635,20 @@ pi_result piProgramCreate(pi_context Context, const void *IL, size_t Length,
1634
1635
1635
1636
assert (Context);
1636
1637
assert (Program);
1637
- ze_device_handle_t ZeDevice = Context->Device ->ZeDevice ;
1638
1638
1639
+ // NOTE: the L0 module creation is also building the program, so we are
1640
+ // deferring it until the program is ready to be built in piProgramBuild
1641
+ // and piProgramCompile. Also it is only then we know the build options.
1642
+ //
1639
1643
ze_module_desc_t ZeModuleDesc = {};
1640
1644
ZeModuleDesc.version = ZE_MODULE_DESC_VERSION_CURRENT;
1641
1645
ZeModuleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
1642
1646
ZeModuleDesc.inputSize = Length;
1643
- ZeModuleDesc.pInputModule = pi_cast<const uint8_t *>(IL);
1644
- ZeModuleDesc.pBuildFlags = nullptr ;
1645
-
1646
- ze_module_handle_t ZeModule;
1647
- ZE_CALL (zeModuleCreate (ZeDevice, &ZeModuleDesc, &ZeModule,
1648
- 0 )); // TODO: handle build log
1647
+ ZeModuleDesc.pInputModule = new uint8_t [Length];
1648
+ memcpy (const_cast <uint8_t *>(ZeModuleDesc.pInputModule ), IL, Length);
1649
1649
1650
1650
try {
1651
- auto ZePiProgram = new _pi_program (ZeModule, Context);
1652
- *Program = pi_cast<pi_program>(ZePiProgram);
1651
+ *Program = new _pi_program (nullptr , ZeModuleDesc, Context);
1653
1652
} catch (const std::bad_alloc &) {
1654
1653
return PI_OUT_OF_HOST_MEMORY;
1655
1654
} catch (...) {
@@ -1670,7 +1669,6 @@ pi_result piProgramCreateWithBinary(pi_context Context, pi_uint32 NumDevices,
1670
1669
assert (Context);
1671
1670
assert (RetProgram);
1672
1671
assert (DeviceList && DeviceList[0 ] == Context->Device );
1673
- ze_device_handle_t ZeDevice = Context->Device ->ZeDevice ;
1674
1672
1675
1673
// Check the binary too.
1676
1674
assert (Lengths && Lengths[0 ] != 0 );
@@ -1682,15 +1680,11 @@ pi_result piProgramCreateWithBinary(pi_context Context, pi_uint32 NumDevices,
1682
1680
ZeModuleDesc.version = ZE_MODULE_DESC_VERSION_CURRENT;
1683
1681
ZeModuleDesc.format = ZE_MODULE_FORMAT_NATIVE;
1684
1682
ZeModuleDesc.inputSize = Length;
1685
- ZeModuleDesc.pInputModule = Binary;
1686
- ZeModuleDesc.pBuildFlags = nullptr ;
1687
-
1688
- ze_module_handle_t ZeModule;
1689
- ZE_CALL (zeModuleCreate (ZeDevice, &ZeModuleDesc, &ZeModule, 0 ));
1683
+ ZeModuleDesc.pInputModule = new uint8_t [Length];
1684
+ memcpy (const_cast <uint8_t *>(ZeModuleDesc.pInputModule ), Binary, Length);
1690
1685
1691
1686
try {
1692
- auto ZePiProgram = new _pi_program (ZeModule, Context);
1693
- *RetProgram = pi_cast<pi_program>(ZePiProgram);
1687
+ *RetProgram = new _pi_program (nullptr , ZeModuleDesc, Context);
1694
1688
} catch (const std::bad_alloc &) {
1695
1689
return PI_OUT_OF_HOST_MEMORY;
1696
1690
} catch (...) {
@@ -1777,14 +1771,7 @@ pi_result piProgramLink(pi_context Context, pi_uint32 NumDevices,
1777
1771
const pi_program *InputPrograms,
1778
1772
void (*PFnNotify)(pi_program Program, void *UserData),
1779
1773
void *UserData, pi_program *RetProgram) {
1780
- die (" piProgramLink: Program Linking is not supported yet in Level0" );
1781
-
1782
- // TODO: L0 builds the program at the time of piProgramCreate.
1783
- // But build options are not available at that time, so we must
1784
- // stop building it there, but move it here. The problem though
1785
- // is that this would mean moving zeModuleCreate here entirely,
1786
- // and so L0 module creation would be deferred until
1787
- // piProgramCompile/piProgramLink/piProgramBuild.
1774
+ // TODO: L0 does not [yet] support linking so dummy implementation here.
1788
1775
assert (NumInputPrograms == 1 && InputPrograms);
1789
1776
assert (RetProgram);
1790
1777
*RetProgram = InputPrograms[0 ];
@@ -1797,32 +1784,36 @@ pi_result piProgramCompile(
1797
1784
const pi_program *InputHeaders, const char **HeaderIncludeNames,
1798
1785
void (*PFnNotify)(pi_program Program, void *UserData), void *UserData) {
1799
1786
1800
- // TODO: L0 builds the program at the time of piProgramCreate.
1801
- // But build options are not available at that time, so we must
1802
- // stop building it there, but move it here. The problem though
1803
- // is that this would mean moving zeModuleCreate here entirely,
1804
- // and so L0 module creation would be deferred until
1805
- // piProgramCompile/piProgramLink/piProgramBuild.
1806
- //
1807
- // It is expected that program was successfully built during piProgramCreate
1808
- assert (Program && Program->ZeModule );
1809
- return PI_SUCCESS;
1787
+ // Assert on unsupported arguments.
1788
+ assert (NumInputHeaders == 0 );
1789
+ assert (!InputHeaders);
1790
+
1791
+ // There is no support foe linking yet in L0 so "compile" actually
1792
+ // does the "build".
1793
+ return piProgramBuild (Program, NumDevices, DeviceList, Options, PFnNotify,
1794
+ UserData);
1810
1795
}
1811
1796
1812
1797
pi_result piProgramBuild (pi_program Program, pi_uint32 NumDevices,
1813
1798
const pi_device *DeviceList, const char *Options,
1814
1799
void (*PFnNotify)(pi_program Program, void *UserData),
1815
1800
void *UserData) {
1801
+ assert (Program);
1802
+ assert (NumDevices == 1 );
1803
+ assert (DeviceList && DeviceList[0 ] == Program->Context ->Device );
1804
+ assert (!PFnNotify);
1805
+ assert (!UserData);
1816
1806
1817
- // TODO: L0 builds the program at the time of piProgramCreate.
1818
- // But build options are not available at that time, so we must
1819
- // stop building it there, but move it here. The problem though
1820
- // is that this would mean moving zeModuleCreate here entirely,
1821
- // and so L0 module creation would be deferred until
1822
- // piProgramCompile/piProgramLink/piProgramBuild.
1823
- //
1824
- // It is expected that program was successfully built during piProgramCreate
1825
- assert (Program && Program->ZeModule );
1807
+ // Check that the program wasn't already built.
1808
+ assert (!Program->ZeModule );
1809
+
1810
+ Program->ZeModuleDesc .pBuildFlags = Options;
1811
+ // TODO: set specialization constants here.
1812
+ Program->ZeModuleDesc .pConstants = nullptr ;
1813
+
1814
+ ze_device_handle_t ZeDevice = Program->Context ->Device ->ZeDevice ;
1815
+ ZE_CALL (zeModuleCreate (ZeDevice, &Program->ZeModuleDesc , &Program->ZeModule ,
1816
+ &Program->ZeBuildLog ));
1826
1817
return PI_SUCCESS;
1827
1818
}
1828
1819
@@ -1846,9 +1837,19 @@ pi_result piProgramGetBuildInfo(pi_program Program, pi_device Device,
1846
1837
// return for programs that were built outside and registered
1847
1838
// with piProgramRegister?
1848
1839
return ReturnValue (" " );
1840
+ } else if (ParamName == CL_PROGRAM_BUILD_LOG) {
1841
+ assert (Program->ZeBuildLog );
1842
+ size_t LogSize = ParamValueSize;
1843
+ ZE_CALL (zeModuleBuildLogGetString (Program->ZeBuildLog , &LogSize,
1844
+ pi_cast<char *>(ParamValue)));
1845
+ if (ParamValueSizeRet) {
1846
+ *ParamValueSizeRet = LogSize;
1847
+ }
1848
+ } else {
1849
+ zePrint (" piProgramGetBuildInfo: unsupported ParamName\n " );
1850
+ return PI_INVALID_VALUE;
1849
1851
}
1850
- zePrint (" piProgramGetBuildInfo: unsupported ParamName\n " );
1851
- return PI_INVALID_VALUE;
1852
+ return PI_SUCCESS;
1852
1853
}
1853
1854
1854
1855
pi_result piProgramRetain (pi_program Program) {
@@ -1861,6 +1862,9 @@ pi_result piProgramRelease(pi_program Program) {
1861
1862
assert (Program);
1862
1863
assert ((Program->RefCount > 0 ) && " Program is already released." );
1863
1864
if (--(Program->RefCount ) == 0 ) {
1865
+ delete[] Program->ZeModuleDesc .pInputModule ;
1866
+ if (Program->ZeBuildLog )
1867
+ zeModuleBuildLogDestroy (Program->ZeBuildLog );
1864
1868
// TODO: call zeModuleDestroy for non-interop L0 modules
1865
1869
delete Program;
1866
1870
}
0 commit comments