From markro at cs.washington.edu Wed May 7 17:43:46 2025 From: markro at cs.washington.edu (Mark Roberts) Date: Wed, 7 May 2025 10:43:46 -0700 Subject: are CodeElements unique? Message-ID: <38c4f632a1700c758245c52273f24070@mail.gmail.com> Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul.sandoz at oracle.com Wed May 7 17:59:49 2025 From: paul.sandoz at oracle.com (Paul Sandoz) Date: Wed, 7 May 2025 17:59:49 +0000 Subject: are CodeElements unique? In-Reply-To: <38c4f632a1700c758245c52273f24070@mail.gmail.com> References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> Message-ID: <507610BE-7D8B-49FA-852A-7DDD798E5872@oracle.com> Yes. All code items have identity, and their equals/hashCode is defined in terms of that identity i.e., same implementation as for Object. A reflected method body or lambda body must have only one code model instance associated with it. (We have been fixing some a bugs to ensure this is guaranteed.) Paul. On May 7, 2025, at 10:43?AM, Mark Roberts wrote: Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul.sandoz at oracle.com Wed May 7 18:00:23 2025 From: paul.sandoz at oracle.com (Paul Sandoz) Date: Wed, 7 May 2025 18:00:23 +0000 Subject: are CodeElements unique? In-Reply-To: <507610BE-7D8B-49FA-852A-7DDD798E5872@oracle.com> References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> <507610BE-7D8B-49FA-852A-7DDD798E5872@oracle.com> Message-ID: Oops apologies, I thought this was a Babylon code model question, please ignore! Paul. On May 7, 2025, at 10:59?AM, paul.sandoz at oracle.com wrote: Yes. All code items have identity, and their equals/hashCode is defined in terms of that identity i.e., same implementation as for Object. A reflected method body or lambda body must have only one code model instance associated with it. (We have been fixing some a bugs to ensure this is guaranteed.) Paul. On May 7, 2025, at 10:43?AM, Mark Roberts wrote: Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From chen.l.liang at oracle.com Wed May 7 18:02:45 2025 From: chen.l.liang at oracle.com (Chen Liang) Date: Wed, 7 May 2025 18:02:45 +0000 Subject: [External] : are CodeElements unique? In-Reply-To: <38c4f632a1700c758245c52273f24070@mail.gmail.com> References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> Message-ID: Hi Mark, Very good question - and the answer is unfortunately, no. Transforms are allowed to reuse code models, and I think the bound Code attribute expansion also returns duplicate load or store instructions. To ensure identity, I think tracking a pair of (index, element) is better. Regards, Chen Liang ________________________________ From: Mark Roberts Sent: Wednesday, May 7, 2025 12:43 PM To: classfile-api-dev at openjdk.org Cc: Chen Liang Subject: [External] : are CodeElements unique? Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From chen.l.liang at oracle.com Wed May 7 19:00:36 2025 From: chen.l.liang at oracle.com (Chen Liang) Date: Wed, 7 May 2025 19:00:36 +0000 Subject: are CodeElements unique? In-Reply-To: References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> <507610BE-7D8B-49FA-852A-7DDD798E5872@oracle.com> Message-ID: No worries - this actually reminds me of Adam's complaints about ClassFile API's parsed instructions having no identity and thus complicated to handle for the low code model in Babylon. Identity is important for control flow and transforms like SSA transform. Unfortunately, since transforms are free to pass in any element or duplicate them, we probably can't do that for ClassFile API, and have to resort to such an external mechanism at the terminals. Regards, Chen ________________________________ From: Paul Sandoz Sent: Wednesday, May 7, 2025 1:00 PM Cc: Mark Roberts ; classfile-api-dev at openjdk.org ; Chen Liang Subject: Re: are CodeElements unique? Oops apologies, I thought this was a Babylon code model question, please ignore! Paul. On May 7, 2025, at 10:59?AM, paul.sandoz at oracle.com wrote: Yes. All code items have identity, and their equals/hashCode is defined in terms of that identity i.e., same implementation as for Object. A reflected method body or lambda body must have only one code model instance associated with it. (We have been fixing some a bugs to ensure this is guaranteed.) Paul. On May 7, 2025, at 10:43?AM, Mark Roberts wrote: Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From markro at cs.washington.edu Wed May 7 19:06:44 2025 From: markro at cs.washington.edu (Mark Roberts) Date: Wed, 7 May 2025 12:06:44 -0700 Subject: [External] : are CodeElements unique? In-Reply-To: References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> Message-ID: <7762eed170be88c6e3368caef2f9a1e0@mail.gmail.com> How about only for the original, immutable elementlist prior to any transforms? Also, not sure what you meant by a (index, element) pair. Thanks, Mark *From:* Chen Liang *Sent:* Wednesday, May 7, 2025 11:03 AM *To:* Mark Roberts ; classfile-api-dev at openjdk.org *Subject:* Re: [External] : are CodeElements unique? Hi Mark, Very good question - and the answer is unfortunately, no. Transforms are allowed to reuse code models, and I think the bound Code attribute expansion also returns duplicate load or store instructions. To ensure identity, I think tracking a pair of (index, element) is better. Regards, Chen Liang ------------------------------ *From:* Mark Roberts *Sent:* Wednesday, May 7, 2025 12:43 PM *To:* classfile-api-dev at openjdk.org *Cc:* Chen Liang *Subject:* [External] : are CodeElements unique? Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.sotona at oracle.com Mon May 12 09:56:26 2025 From: adam.sotona at oracle.com (Adam Sotona) Date: Mon, 12 May 2025 09:56:26 +0000 Subject: [External] : are CodeElements unique? In-Reply-To: <7762eed170be88c6e3368caef2f9a1e0@mail.gmail.com> References: <38c4f632a1700c758245c52273f24070@mail.gmail.com> <7762eed170be88c6e3368caef2f9a1e0@mail.gmail.com> Message-ID: Hi Mark, Code elements are generally not unique, so they cannot be used as keys in maps. However, there are several approaches to work around it (based on the use case): * If you need to iterate over a list of elements multiple times and then transform it ? you can use the index of the element as a key. * Or if you have a complex set of chained and nested transformations and need to mark a code element for later identification, you can bind a label (CodeBuilder::newBoundLabel) to a position before, after, or instead of the element. Labels are unique, and your subsequent transformations will receive a LabelTarget pseudo-instruction at the marked point, and LabelTarget::label will match the label you bound earlier. Adam From: classfile-api-dev on behalf of Mark Roberts Date: Wednesday, 7 May 2025 at 21:06 To: Chen Liang , classfile-api-dev at openjdk.org Subject: RE: [External] : are CodeElements unique? How about only for the original, immutable elementlist prior to any transforms? Also, not sure what you meant by a (index, element) pair. Thanks, Mark From: Chen Liang > Sent: Wednesday, May 7, 2025 11:03 AM To: Mark Roberts >; classfile-api-dev at openjdk.org Subject: Re: [External] : are CodeElements unique? Hi Mark, Very good question - and the answer is unfortunately, no. Transforms are allowed to reuse code models, and I think the bound Code attribute expansion also returns duplicate load or store instructions. To ensure identity, I think tracking a pair of (index, element) is better. Regards, Chen Liang ________________________________ From: Mark Roberts > Sent: Wednesday, May 7, 2025 12:43 PM To: classfile-api-dev at openjdk.org > Cc: Chen Liang > Subject: [External] : are CodeElements unique? Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map? Thank you, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: