From 7217fd7a540d22a6eee153f15bffea2beceea725 Mon Sep 17 00:00:00 2001 From: Petr Vesely Date: Mon, 18 Sep 2023 17:43:45 +0100 Subject: [PATCH 1/2] [UR] Support Raw C Arrays --- include/ur.py | 4 +-- include/ur_api.h | 10 +++----- scripts/core/exp-bindless-images.yml | 12 +++------ scripts/templates/helper.py | 38 ++++++++++++++++++++++++---- scripts/templates/params.hpp.mako | 9 +++++++ source/common/ur_params.hpp | 21 ++++++--------- test/unit/utils/params.cpp | 27 +++++++++++++++++++- 7 files changed, 84 insertions(+), 37 deletions(-) diff --git a/include/ur.py b/include/ur.py index a1f4f0fd2d..2da7631697 100644 --- a/include/ur.py +++ b/include/ur.py @@ -2228,9 +2228,7 @@ class ur_exp_sampler_addr_modes_t(Structure): ("stype", ur_structure_type_t), ## [in] type of this structure, must be ## ::UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES ("pNext", c_void_p), ## [in,out][optional] pointer to extension-specific structure - ("addrModeX", ur_sampler_addressing_mode_t), ## [in] Specify the addressing mode of the x-dimension. - ("addrModeY", ur_sampler_addressing_mode_t), ## [in] Specify the addressing mode of the y-dimension. - ("addrModeZ", ur_sampler_addressing_mode_t) ## [in] Specify the addressing mode of the z-dimension. + ("addrModes", ur_sampler_addressing_mode_t * 3) ## [in] Specify the address mode of the sampler per dimension ] ############################################################################### diff --git a/include/ur_api.h b/include/ur_api.h index e8e770914d..5765df1d86 100644 --- a/include/ur_api.h +++ b/include/ur_api.h @@ -7047,12 +7047,10 @@ typedef struct ur_exp_sampler_mip_properties_t { /// - Specify these properties in ::urSamplerCreate via ::ur_sampler_desc_t /// as part of a `pNext` chain. typedef struct ur_exp_sampler_addr_modes_t { - ur_structure_type_t stype; ///< [in] type of this structure, must be - ///< ::UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES - void *pNext; ///< [in,out][optional] pointer to extension-specific structure - ur_sampler_addressing_mode_t addrModeX; ///< [in] Specify the addressing mode of the x-dimension. - ur_sampler_addressing_mode_t addrModeY; ///< [in] Specify the addressing mode of the y-dimension. - ur_sampler_addressing_mode_t addrModeZ; ///< [in] Specify the addressing mode of the z-dimension. + ur_structure_type_t stype; ///< [in] type of this structure, must be + ///< ::UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES + void *pNext; ///< [in,out][optional] pointer to extension-specific structure + ur_sampler_addressing_mode_t addrModes[3]; ///< [in] Specify the address mode of the sampler per dimension } ur_exp_sampler_addr_modes_t; diff --git a/scripts/core/exp-bindless-images.yml b/scripts/core/exp-bindless-images.yml index fe74fb1c5c..b5f87a6633 100644 --- a/scripts/core/exp-bindless-images.yml +++ b/scripts/core/exp-bindless-images.yml @@ -187,15 +187,9 @@ class: $xBindlessImages name: $x_exp_sampler_addr_modes_t base: $x_base_properties_t members: - - type: $x_sampler_addressing_mode_t - name: addrModeX - desc: "[in] Specify the addressing mode of the x-dimension." - - type: $x_sampler_addressing_mode_t - name: addrModeY - desc: "[in] Specify the addressing mode of the y-dimension." - - type: $x_sampler_addressing_mode_t - name: addrModeZ - desc: "[in] Specify the addressing mode of the z-dimension." + - type: $x_sampler_addressing_mode_t[3] + name: addrModes + desc: "[in] Specify the address mode of the sampler per dimension" --- #-------------------------------------------------------------------------- type: struct desc: "Describes an interop memory resource descriptor" diff --git a/scripts/templates/helper.py b/scripts/templates/helper.py index 2b283b8119..15a440da3d 100644 --- a/scripts/templates/helper.py +++ b/scripts/templates/helper.py @@ -105,6 +105,7 @@ class type_traits: RE_DESC = r"(.*)desc_t.*" RE_PROPS = r"(.*)properties_t.*" RE_FLAGS = r"(.*)flags_t" + RE_ARRAY = r"(.*)\[([1-9][0-9]*)\]" @staticmethod def base(name): @@ -217,6 +218,29 @@ def find_class_name(name, meta): except: return None + @classmethod + def is_array(cls, name): + try: + return True if re.match(cls.RE_ARRAY, name) else False + except: + return False + + @classmethod + def get_array_length(cls, name): + if not cls.is_array(name): + raise Exception("Cannot find array length of non-array type.") + + match = re.match(cls.RE_ARRAY, name) + return match.groups()[1] + + @classmethod + def get_array_element_type(cls, name): + if not cls.is_array(name): + raise Exception("Cannot find array length of non-array type.") + + match = re.match(cls.RE_ARRAY, name) + return match.groups()[0] + """ Extracts traits from a value name """ @@ -729,7 +753,10 @@ def make_etor_lines(namespace, tags, obj, py=False, meta=None): returns c/c++ name of any type """ def _get_type_name(namespace, tags, obj, item): - name = subt(namespace, tags, item['type'],) + type = item['type'] + if type_traits.is_array(type): + type = type_traits.get_array_element_type(type) + name = subt(namespace, tags, type,) return name """ @@ -763,9 +790,9 @@ def get_ctype_name(namespace, tags, item): while type_traits.is_pointer(name): name = "POINTER(%s)"%_remove_ptr(name) - if 'name' in item and value_traits.is_array(item['name']): - length = subt(namespace, tags, value_traits.get_array_length(item['name'])) - name = "%s * %s"%(name, length) + if 'name' in item and type_traits.is_array(item['type']): + length = subt(namespace, tags, type_traits.get_array_length(item['type'])) + name = "%s * %s"%(type_traits.get_array_element_type(name), length) return name @@ -804,7 +831,8 @@ def make_member_lines(namespace, tags, obj, prefix="", py=False, meta=None): delim = "," if i < (len(obj['members'])-1) else "" prologue = "(\"%s\", %s)%s"%(name, tname, delim) else: - prologue = "%s %s;"%(tname, name) + array_suffix = f"[{type_traits.get_array_length(item['type'])}]" if type_traits.is_array(item['type']) else "" + prologue = "%s %s %s;"%(tname, name, array_suffix) comment_style = "##" if py else "///<" ws_count = 64 if py else 48 diff --git a/scripts/templates/params.hpp.mako b/scripts/templates/params.hpp.mako index de971c481f..9609a0c211 100644 --- a/scripts/templates/params.hpp.mako +++ b/scripts/templates/params.hpp.mako @@ -84,6 +84,15 @@ def findMemberType(_item): %elif findMemberType(item) is not None and findMemberType(item)['type'] == "union": os << ".${iname} = "; ${x}_params::serializeUnion(os, ${deref}(params${access}${item['name']}), params${access}${th.param_traits.tagged_member(item)}); + %elif th.type_traits.is_array(item['type']): + os << ".${iname} = ["; + for(auto i = 0; i < ${th.type_traits.get_array_length(item['type'])}; i++){ + if(i != 0){ + os << ", "; + } + os << ${deref}(params${access}${item['name']}[i]); + } + os << "]"; %elif typename is not None: os << ".${iname} = "; ${x}_params::serializeTagged(os, ${deref}(params${access}${pname}), ${deref}(params${access}${prefix}${typename}), ${deref}(params${access}${prefix}${typename_size})); diff --git a/source/common/ur_params.hpp b/source/common/ur_params.hpp index b852c72e7c..02a29a1a8f 100644 --- a/source/common/ur_params.hpp +++ b/source/common/ur_params.hpp @@ -9905,19 +9905,14 @@ operator<<(std::ostream &os, const struct ur_exp_sampler_addr_modes_t params) { ur_params::serializeStruct(os, (params.pNext)); os << ", "; - os << ".addrModeX = "; - - os << (params.addrModeX); - - os << ", "; - os << ".addrModeY = "; - - os << (params.addrModeY); - - os << ", "; - os << ".addrModeZ = "; - - os << (params.addrModeZ); + os << ".addrModes = ["; + for (auto i = 0; i < 3; i++) { + if (i != 0) { + os << ", "; + } + os << (params.addrModes[i]); + } + os << "]"; os << "}"; return os; diff --git a/test/unit/utils/params.cpp b/test/unit/utils/params.cpp index d54263591b..d3a049d5a9 100644 --- a/test/unit/utils/params.cpp +++ b/test/unit/utils/params.cpp @@ -367,6 +367,31 @@ struct UrDevicePartitionPropertyTest { ur_device_partition_property_t prop; }; +struct UrSamplerAddressModesTest { + UrSamplerAddressModesTest() { + prop.addrModes[0] = UR_SAMPLER_ADDRESSING_MODE_CLAMP; + prop.addrModes[1] = UR_SAMPLER_ADDRESSING_MODE_MIRRORED_REPEAT; + prop.addrModes[2] = UR_SAMPLER_ADDRESSING_MODE_REPEAT; + prop.pNext = nullptr; + prop.stype = UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES; + } + ur_exp_sampler_addr_modes_t &get_struct() { return prop; } + const char *get_expected() { + return "\\(struct ur_exp_sampler_addr_modes_t\\)" + "\\{" + ".stype = UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES, " + ".pNext = nullptr, " + ".addrModes = \\[" + "UR_SAMPLER_ADDRESSING_MODE_CLAMP, " + "UR_SAMPLER_ADDRESSING_MODE_MIRRORED_REPEAT, " + "UR_SAMPLER_ADDRESSING_MODE_REPEAT" + "\\]" + "\\}"; + } + + ur_exp_sampler_addr_modes_t prop; +}; + using testing::Types; typedef Types + UrDevicePartitionPropertyTest, UrSamplerAddressModesTest> Implementations; using ::testing::MatchesRegex; From e9aa0f4e1108a8ff4ce0d5ed65510ad9954622a1 Mon Sep 17 00:00:00 2001 From: Petr Vesely Date: Tue, 19 Sep 2023 11:38:29 +0100 Subject: [PATCH 2/2] [UR] Address Review Feedback --- scripts/templates/helper.py | 2 +- scripts/templates/params.hpp.mako | 8 +++++--- source/common/ur_params.hpp | 5 +++-- test/unit/utils/params.cpp | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts/templates/helper.py b/scripts/templates/helper.py index 15a440da3d..e6e3a8569e 100644 --- a/scripts/templates/helper.py +++ b/scripts/templates/helper.py @@ -236,7 +236,7 @@ def get_array_length(cls, name): @classmethod def get_array_element_type(cls, name): if not cls.is_array(name): - raise Exception("Cannot find array length of non-array type.") + raise Exception("Cannot find array type of non-array type.") match = re.match(cls.RE_ARRAY, name) return match.groups()[0] diff --git a/scripts/templates/params.hpp.mako b/scripts/templates/params.hpp.mako index 9609a0c211..30d39a9e9a 100644 --- a/scripts/templates/params.hpp.mako +++ b/scripts/templates/params.hpp.mako @@ -85,14 +85,16 @@ def findMemberType(_item): os << ".${iname} = "; ${x}_params::serializeUnion(os, ${deref}(params${access}${item['name']}), params${access}${th.param_traits.tagged_member(item)}); %elif th.type_traits.is_array(item['type']): - os << ".${iname} = ["; + os << ".${iname} = {"; for(auto i = 0; i < ${th.type_traits.get_array_length(item['type'])}; i++){ if(i != 0){ os << ", "; } - os << ${deref}(params${access}${item['name']}[i]); + <%call expr="member(iname, itype, True)"> + ${deref}(params${access}${item['name']}[i]) + } - os << "]"; + os << "}"; %elif typename is not None: os << ".${iname} = "; ${x}_params::serializeTagged(os, ${deref}(params${access}${pname}), ${deref}(params${access}${prefix}${typename}), ${deref}(params${access}${prefix}${typename_size})); diff --git a/source/common/ur_params.hpp b/source/common/ur_params.hpp index 02a29a1a8f..b2d78ee0b6 100644 --- a/source/common/ur_params.hpp +++ b/source/common/ur_params.hpp @@ -9905,14 +9905,15 @@ operator<<(std::ostream &os, const struct ur_exp_sampler_addr_modes_t params) { ur_params::serializeStruct(os, (params.pNext)); os << ", "; - os << ".addrModes = ["; + os << ".addrModes = {"; for (auto i = 0; i < 3; i++) { if (i != 0) { os << ", "; } + os << (params.addrModes[i]); } - os << "]"; + os << "}"; os << "}"; return os; diff --git a/test/unit/utils/params.cpp b/test/unit/utils/params.cpp index d3a049d5a9..d6310f8bbd 100644 --- a/test/unit/utils/params.cpp +++ b/test/unit/utils/params.cpp @@ -381,11 +381,11 @@ struct UrSamplerAddressModesTest { "\\{" ".stype = UR_STRUCTURE_TYPE_EXP_SAMPLER_ADDR_MODES, " ".pNext = nullptr, " - ".addrModes = \\[" + ".addrModes = \\{" "UR_SAMPLER_ADDRESSING_MODE_CLAMP, " "UR_SAMPLER_ADDRESSING_MODE_MIRRORED_REPEAT, " "UR_SAMPLER_ADDRESSING_MODE_REPEAT" - "\\]" + "\\}" "\\}"; }