IP Library Granted Patent US 12,379,928
Granted Patent B2
US 12,379,928 · App. 18/320,039 · Granted Aug 5, 2025

Method and apparatus for rectifying weak memory ordering problem

Inventors: Di Yu (Shenzhen, CN); Yandong Lv (Shanghai, CN); Rutao Zhang (Shanghai, CN)
Assignee: Huawei Technologies Co., Ltd.
G06F9/30192G06F8/458G06F9/3004G06F9/30087G06F9/38G06F9/3834G06F9/3856G06F8/41G06F9/3851
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12,379,928
App. No.
18/320,039
Granted
Aug 5, 2025
Kind
B2
Abstract

This application relates to the field of computer technologies, and discloses methods and apparatuses, for example, for rectifying a weak memory ordering problem. An example method includes: determining a read/write instruction set in to-be-repaired code; classifying instructions in the read/write instruction set to determine a target instruction; and inserting a memory barrier instruction between a previous read/write instruction of the target instruction and the target instruction. The read/write instruction set includes a read instruction and/or a write instruction in the to-be-repaired code, and an instruction in the read/write instruction set is used for memory access.

Claims (57)

1. A method, wherein the method comprises:

determining a read/write instruction set in to-be-repaired code, wherein the read/write instruction set comprises a read instruction or a write instruction in the to-be-repaired code, and wherein an instruction in the read/write instruction set is used for memory access;

classifying instructions in the read/write instruction set to determine a target instruction; and

inserting a memory barrier instruction between a previous read/write instruction of the target instruction and the target instruction.

2. The method according to claim 1 , wherein the classifying instructions in the read/write instruction set to determine a target instruction specifically comprises:

determining a first-type instruction in the read/write instruction set, wherein the first-type instruction is an instruction with weak memory ordering security; and

determining the target instruction according to the first-type instruction, wherein the target instruction is an instruction in the read/write instruction set other than the first-type instruction.

3. The method according to claim 2 , wherein the determining the target instruction according to the first-type instruction specifically comprises:

determining a target instruction set according to the first-type instruction, wherein the target instruction set comprises all instructions in the read/write instruction set except the first-type instruction; and

determining the target instruction in the target instruction set according to an optimization policy, wherein the optimization policy indicates a type of a read/write instruction having a to-be-repaired weak memory ordering problem.

4. The method according to claim 1 , wherein the classifying instructions in the read/write instruction set to determine a target instruction specifically comprises:

classifying the instructions in the read/write instruction set according to an optimization policy to determine the target instruction, wherein the optimization policy indicates a type of a read/write instruction having a to-be-repaired weak memory ordering problem.

5. The method according to claim 4 , wherein the optimization policy comprises at least one of:

a first policy for determining a read/write instruction that is in the target instruction set and that belongs to a whitelist as the target instruction; or determining a read/write instruction that is in the target instruction set and that is other than a read/write instruction in a blacklist as the target instruction;

a second policy for determining, as the target instruction, a read/write instruction serving only as a dependency in read/write instructions having a context dependency relationship in the target instruction set;

a third policy for determining a read/write instruction in the target instruction set other than a read/write instruction related to a special-purpose register as the target instruction; or

a fourth policy for determining a read/write instruction in the target instruction set other than a read/write instruction related to an input/output parameter as the target instruction.

6. The method according to claim 5 , wherein the method further comprises:

obtaining a fifth policy configured by a user; and

adding the fifth policy to the optimization policy.

7. The method according to claim 4 , wherein before the determining the target instruction, the method further comprises:

receiving indication information, wherein the indication information indicates a user to select the optimization policy from to-be-selected optimization policies.

8. The method according to claim 2 , wherein the first-type instruction comprises at least one of an instruction for reading/writing an inter-thread non-shared variable, or a read/write instruction with order-preserving semantics.

9. The method according to claim 1 , wherein the determining a read/write instruction set specifically comprises:

identifying an instruction in a target function to determine the read/write instruction set, wherein the target function is any function in the to-be-repaired code.

10. The method according to claim 1 , wherein:

the to-be-repaired code is in an intermediate language obtained by compiling source code of a multithreaded program by a compiler; or

the to-be-repaired code is assembly code obtained by compiling source code of a multithreaded program by a compiler.

11. An apparatus, wherein the apparatus comprises one or more memories and one or more processors, and the one or more memories store computer instructions for execution by the one or more processors to:

determine a read/write instruction set in to-be-repaired code, wherein the read/write instruction set comprises a read instruction or a write instruction in the to-be-repaired code, and wherein an instruction in the read/write instruction set is used for memory access;

classify instructions in the read/write instruction set to determine a target instruction; and

insert a memory barrier instruction between a previous read/write instruction of the target instruction and the target instruction.

12. The apparatus of claim 11 , wherein the computer instructions are for execution by the one or more processors to:

determine a first-type instruction in the read/write instruction set, wherein the first-type instruction is an instruction with weak memory ordering security; and

determine the target instruction according to the first-type instruction, wherein the target instruction is an instruction in the read/write instruction set other than the first-type instruction.

13. The apparatus of claim 12 , wherein the computer instructions are for execution by the one or more processors to:

determine a target instruction set according to the first-type instruction, wherein the target instruction set comprises all instructions in the read/write instruction set except the first-type instruction; and

determine the target instruction in the target instruction set according to an optimization policy, wherein the optimization policy indicates a type of a read/write instruction having a to-be-repaired weak memory ordering problem.

14. The apparatus of claim 11 , wherein the computer instructions are for execution by the one or more processors to:

classify the instructions in the read/write instruction set according to an optimization policy to determine the target instruction, wherein the optimization policy indicates a type of a read/write instruction having a to-be-repaired weak memory ordering problem.

15. The apparatus of claim 14 , wherein the optimization policy comprises at least one of:

a first policy for determining a read/write instruction that is in the target instruction set and that belongs to a whitelist as the target instruction; or determining a read/write instruction that is in the target instruction set and that is other than a read/write instruction in a blacklist as the target instruction;

a second policy for determining, as the target instruction, a read/write instruction serving only as a dependency in read/write instructions having a context dependency relationship in the target instruction set;

a third policy for determining a read/write instruction in the target instruction set other than a read/write instruction related to a special-purpose register as the target instruction; or

a fourth policy for determining a read/write instruction in the target instruction set other than a read/write instruction related to an input/output parameter as the target instruction.

16. The apparatus of claim 15 , wherein the computer instructions are for execution by the one or more processors to:

obtain a fifth policy configured by a user; and

add the fifth policy to the optimization policy.

17. The apparatus of claim 14 , wherein the computer instructions are for execution by the one or more processors to:

receive indication information, wherein the indication information indicates a user to select the optimization policy from to-be-selected optimization policies.

18. The apparatus of claim 12 , wherein the first-type instruction comprises at least one of an instruction for reading/writing an inter-thread non-shared variable, or a read/write instruction with order-preserving semantics.

19. The apparatus of claim 12 , wherein the computer instructions are for execution by the one or more processors to:

identify an instruction in a target function to determine the read/write instruction set, wherein the target function is any function in the to-be-repaired code.

20. A non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores a computer program; and when the computer program is run on a computer, the computer is enabled to perform:

determining a read/write instruction set in to-be-repaired code, wherein the read/write instruction set comprises a read instruction or a write instruction in the to-be-repaired code, and wherein an instruction in the read/write instruction set is used for memory access;

classifying instructions in the read/write instruction set to determine a target instruction; and

inserting a memory barrier instruction between a previous read/write instruction of the target instruction and the target instruction.

Assignments (1)
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Jul 1, 2025
From: YU, DI; LV, YANDONG; ZHANG, RUTAO
To: HUAWEI TECHNOLOGIES CO., LTD.
Reel/Frame 071573/0863 →
Priority Claims (1)
CN 202011303988.0 · Nov 19, 2020 · national
Continuity (2)
Continuation PCTCN2021123883 · Oct 14, 2021
Related Publication 20230289187A1 · Sep 14, 2023
References Cited (19)
US 6209073B1 · Okpisz · 2001 [cited by examiner]
US 6973605B1 · Templeton · 2005 [cited by examiner]
US 7447876B2 · Senter · 2008 [cited by examiner]
US 7941627B2 · Arimilli · 2011 [cited by examiner]
US 10417056B2 · Dice · 2019 [cited by examiner]
US 11507379B2 · Mukherjee · 2022 [cited by examiner]
US 11625249B2 · Kotra · 2023 [cited by examiner]
US 20220413945A1 · Ciolkosz · 2022 [cited by examiner]
US 20230315458A1 · Srinath · 2023 [cited by examiner]
CN 101833440A · 2010 [cited by applicant]
De Carvalho et al., “Improving Transactional Code Generation via Variable Annotation and Barrier Elision,” Proceedings of 2020 IEEE International Parallel and Distributed Processing Symposium (IPDPS), May 18, 2020, pp. … [cited by applicant]
Kuperstein et al., “Automatic Inference of Memory Fences,” ACM SIGACT News, Jun. 2012, vol. 43, No. 2, 9 pages. [cited by applicant]
Yang, “A Static Analysis Method for Thread Modularization in Weakly Consistent Model,” Security Model, Algorithm, and Programming, Jan. 31, 2018, 24 pages (with English translation). [cited by applicant]
Li [Online], “C++11:std:memory_order (C++11 Six Memory Sequences),” Aug. 28, 2020, retrieved from URL<https://so.csdn.net/so/search?q=c%2V%2B11%EF%BC%9Astd%3A3Amemory_orderandt=blogandu=weixin_40179091>, 7 pages (with E… [cited by applicant]
De Carvalho, “Optimizing Memory Transactions for Large-Scale Programs,” Thesis for the degree of Doctor of Information Systems and Computer Engineering, University of Lisbon, Jan. 1, 2014, 187 pages. [cited by applicant]
Abdulla et al., “Automatic Fence Insertion in Integer Programs via Predicate Abstraction,” Proceedings of Static Analysis: 19th International Symposium, SAS 2012, Sep. 11-13, 2012, pp. 164-180. [cited by applicant]
International Search Report and Written Opinion in International Appln. No. PCT/CN2021/123883, mailed on Jan. 14, 2022, 15 pages (with English translation). [cited by applicant]
Extended European Search Report in European Appln. No. 21893641.7, mailed on Mar. 25, 2024, 15 pages. [cited by applicant]
Zhuanlan.Zhihu.com [online], “Memory Order,” Nov. 20, 2018, retrieved on May 8, 2025, retrieved from URL <https://zhuanlan.zhihu.com/p/45566448>, 10 pages (with English translation). [cited by applicant]