Identification and management of unsafe optimizations

Techniques for implementing identification and management of unsafe optimizations are disclosed. A method of the disclosure includes receiving, by a managed runtime environment (MRE) executed by a processing device, a notice of misprediction of optimized code, the misprediction occurring during a runtime of the optimized code, determining, by the MRE, whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, compiling, by the MRE, native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized.

TECHNICAL FIELD

The embodiments of the disclosure relate generally to processing devices and, more specifically, relate to identification and management of unsafe optimizations.

BACKGROUND

Modern platform-independent high-level programming languages, such as Java™ and C++, have significant market presence in the computing industry. Typically, programs written in these languages execute in a managed runtime environment (MRE) that provide automatic memory management, type management, thread and synchronization, and dynamic loading facilities.

MREs typically support just-in-time (JIT) compilation. The programs written in high-level programming languages are generally first compiled in codes in a platform-neutral distribution called bytecode. Bytecode is not machine code for any particular computer, and may be portable among computer architectures. The JIT compiler reads the bytecode in many sections and compiles them dynamically into machine language so that the program can run faster (optimized).

In some cases, the optimizations performed by the MRE in compiling bytecode may be unsafe (e.g., a misprediction). For instance, if a memory instruction is reordered, the optimization may be correct most of the time and performance may be improved, but at some point the optimization's assumptions may not hold. The MRE generally handles these mispredictions in the optimizations by discarding the speculative version and falling back to a pessimized version of the code. A problem with this approach is that the processor may lose performance because some optimizations are safe most of the time, with rare occurrence of mispredictions, so it is better not to pessimize these optimizations.

DETAILED DESCRIPTION

Embodiments of the disclosure provide for identification and management of unsafe optimizations. Code of a program may be optimized using a managed runtime environment (MRE). Once a misprediction of the optimized code occurs during runtime, recovery mechanisms may be implemented and the MRE is invoked to manage this situation. Often, when a misprediction (i.e., unsafe optimization) occurs, the processor executing the optimized code can recover to a safe state. For each region of the code that is optimized unsafely, a safe version of the code may be generated as well.

A previous solution issued the non-optimized version of the code and resumed execution. However, if the optimizations fail often, performance suffers using this solution due to many rollbacks/runtime/restart cycles. Another solution for this problem is to completely disable the optimized version of the code and only use the safe version. However, if this solution is applied frequently, the system may benefit less from the unsafe optimizations that are most of the time correct (e.g., may fail infrequently). In order to overcome this and to still obtain the benefits from the most common cases where the optimization does help, embodiments of the disclosure provide techniques for keeping track of the mispredictions, identifying delinquent portions of optimized code, and selectively pessimizing (e.g., removing optimizations on the code) these identified portions of the optimized code. In other words, embodiments of the disclosure isolate particular portions of optimized code that frequently cause unsafe optimizations and disable optimizations for these identified portions of code.

Although the following embodiments may be described with reference to specific integrated circuits, such as in computing platforms or microprocessors, other embodiments are applicable to other types of integrated circuits and logic devices. Similar techniques and teachings of embodiments described herein may be applied to other types of circuits or semiconductor devices. For example, the disclosed embodiments are not limited to desktop computer systems or Ultrabooks™. And may be also used in other devices, such as handheld devices, tablets, other thin notebooks, systems on a chip (SOC) devices, and embedded applications. Some examples of handheld devices include cellular phones, Internet protocol devices, digital cameras, personal digital assistants (PDAs), and handheld PCs. Embedded applications typically include a microcontroller, a digital signal processor (DSP), a system on a chip, network computers (NetPC), set-top boxes, network hubs, wide area network (WAN) switches, or any other system that can perform the functions and operations taught below.

Although the following embodiments are described with reference to a processor, other embodiments are applicable to other types of integrated circuits and logic devices. Similar techniques and teachings of embodiments of the present invention can be applied to other types of circuits or semiconductor devices that can benefit from higher pipeline throughput and improved performance. The teachings of embodiments of the present invention are applicable to any processor or machine that performs data manipulations. However, the present invention is not limited to processors or machines that perform 512 bit, 256 bit, 128 bit, 64 bit, 32 bit, or 16 bit data operations and can be applied to any processor and machine in which manipulation or management of data is performed. In addition, the following description provides examples, and the accompanying drawings show various examples for the purposes of illustration. However, these examples should not be construed in a limiting sense as they are merely intended to provide examples of embodiments of the present invention rather than to provide an exhaustive list of all possible implementations of embodiments of the present invention.

As more computer systems are used in internet, text, and multimedia applications, additional processor support has been introduced over time. In one embodiment, an instruction set may be associated with one or more computer architectures, including data types, instructions, register architecture, addressing modes, memory architecture, interrupt and exception handling, and external input and output (I/O).

In one embodiment, the instruction set architecture (ISA) may be implemented by one or more micro-architectures, which includes processor logic and circuits used to implement one or more instruction sets. Accordingly, processors with different micro-architectures can share at least a portion of a common instruction set. For example, Intel® Pentium 4 processors, Intel® Core™ processors, and processors from Advanced Micro Devices, Inc. of Sunnyvale Calif. implement nearly identical versions of the x86 instruction set (with some extensions that have been added with newer versions), but have different internal designs. Similarly, processors designed by other processor development companies, such as ARM Holdings, Ltd., MIPS, or their licensees or adopters, may share at least a portion a common instruction set, but may include different processor designs. For example, the same register architecture of the ISA may be implemented in different ways in different micro-architectures using new or well-known techniques, including dedicated physical registers, one or more dynamically allocated physical registers using a register renaming mechanism (e.g., the use of a Register Alias Table (RAT), a Reorder Buffer (ROB) and a retirement register file. In one embodiment, registers may include one or more registers, register architectures, register files, or other register sets that may or may not be addressable by a software programmer.

In one embodiment, an instruction may include one or more instruction formats. In one embodiment, an instruction format may indicate various fields (number of bits, location of bits, etc.) to specify, among other things, the operation to be performed and the operand(s) on which that operation is to be performed. Some instruction formats may be further broken defined by instruction templates (or sub formats). For example, the instruction templates of a given instruction format may be defined to have different subsets of the instruction format's fields and/or defined to have a given field interpreted differently. In one embodiment, an instruction is expressed using an instruction format (and, if defined, in a given one of the instruction templates of that instruction format) and specifies or indicates the operation and the operands upon which the operation will operate.

FIG. 1is a block diagram of a micro-architecture for a computing device100implementing identification and management of unsafe optimizations accordance with one embodiment of the disclosure. In one embodiment, computing device100may include, but is not limited to, a laptop, desktop, handheld personal computer (PC), personal digital assistant, engineering workstation, server device, network device, network hub, switch, embedded processor, digital signal processor, graphics device, video game device, set-top box, micro controller, cell phone, portable media player, hand held device, table computing device, and any other various electronic device, for example.

Computing device100may include a hardware block110, memory120, and a multiple (multi)-core processor130. In some embodiment, multi-core processor130may be implemented as a single core processor. In other embodiments, more than one multi-core processor130may be included in computing device100. The multi-core processor103may include any variety of processing devices having a plurality of cores. One embodiment of multi-core processor103is illustrated further below with respect toFIG. 4. The multi-core processor130may be an integrated circuit (ID), such as a semiconductor integrated circuit chip. In some embodiments, additional or different processors may also be present in the computing device100. For example, additional processors may include processors that are heterogeneous or asymmetric to processor130, accelerators (such as, for example, graphics accelerators or digital signal processing (DSP) units), field programmable gate arrays, or any other processor.

As shown inFIG. 1, multi-core processor103includes a plurality of core central processing units (CPUs), which are shown as CPUs135. Of course, additional or fewer processor cores may be used for the CPUs135. It should be understood that the core(s) may support multithreading (executing two or more parallel sets of operations or threads), and may do so in a variety of ways including time sliced multithreading, simultaneous multithreading (where a single physical core provides a logical core for each of the threads that physical core is simultaneously multithreading), or a combination thereof (e.g., time sliced fetching and decoding and simultaneous multithreading thereafter such as in the Intel® Hyperthreading technology).

Hardware110may include integrated circuit chips, such as those selected from integrated circuit chipsets (e.g., graphics memory and I/O controller hub chipsets), although other integrated chips may also, or alternatively, be used. Hardware110may also include devices115, such as controllers, storage devices, media cards (video, sound, etc.), and/or network cards. In some embodiments, devices115may include pseudo-devices that are emulated devices.

Memory120may include one or more of the following types of memory: semiconductor firmware memory, programmable memory, non-volatile memory, read-only memory, electronically-programmable memory, random access memory, flash memory (which may include, for example. NAND or NOR type memory structures), magnetic disk memory, and/or optical disk memory. Machine-readable firmware program instructions may be stored in memory120. These instructions may be accessed and executed by CPUs135to provide operating system OS140.

OS140may include a general OS or an embedded OS. In one embodiment, OS140may execute a managed runtime environment (MRE)150. However, in other embodiments, MRE150may be implemented to run directly on hardware110using multi-core processor130. In some embodiments, MRE150may include its own instruction set and an OS (not shown). MRE150may dynamically load and execute code that is delivered to it in a portable bytecode format. MRE150may also convert the bytecodes into native instructions through interpretation and compilation.

MRE150may include a core virtual machine (VM)160capable of interpreting and/or compiling the received bytecodes into instructions understood by multi-core processor130. For at least one embodiment, the core VM160is a Java™ VM (JVM). For at least another embodiment, the core VM160is a Common Language Infrastructure (CLI) for C++ programs. The core VM160may include an execution engine162that may directly interpret the bytecodes to execute them. The core VM160may also include a compiled code region164that includes memory space that the native code may occupy. Although not shown, the core VM160may also include additional runtime memory areas including a heap, program counter registers, stacks, virtual method tables, and so on.

MRE150also includes a just-in-time (JIT) compiler155. Instead of using the relatively slower interpretation provided by the execution engine162, the MRE150may execute native code generated by the JIT compiler155. The first time a method is invoked, the JIT compiler155may compile the bytecode of the given method into a native code of the multi-core processor130on which the MRE150is executing. In embodiments of the disclosure, the JIT compiler155optimizes bytecode of a program with optimizations (e.g. branch predictions, etc.) to improve processing speed of the program.

The MRE150may further include a runtime code manager170. Runtime code manager dynamically manages the layout of compiled native codes within the compiled code region164. The runtime code manager170utilizes runtime event information to determine that compiled code should be reorganized and to determine the layout for re-organized native codes. In one embodiment, the event information includes information concerning the execution of methods during runtime and is, thus, dynamically generated. The dynamic event information is generated by hardware during a current execution of an application program. That is, the event information reflects actual real-time behavior during current execution of the application program and is not based on information generated during a prior instrumented run of the application program.

In some embodiments, the event information includes an occurrence of a misprediction of optimized code. A misprediction may include an incorrect prediction of a direction that a branch in code may take. When a portion of optimized code results in a misprediction, this portion of optimized code may be referred to as an “unsafe optimization.” When an unsafe optimization occurs, a misprediction manger175of the runtime code manager170is invoked to handle the misprediction resulting from the optimized code generated by MRE150, in order to allow the multi-core processor130to recover to a safe state. In one embodiment, the safe state includes atomic execution of the code region having the unsafe optimization. In other words, the misprediction manager175causes an unoptimized version of the code region to be issued and executed upon resumption of operations of the application program. Furthermore, the misprediction manager175may isolate the portions of code that cause a high number of mispredictions and disable these portions from being optimized in future runs of the application program. To accomplish this, the misprediction manager175may keep track of faults, identify delinquent components and selectively pessimize parts of the optimized region, as discussed further below with respect toFIG. 4.

FIG. 2illustrates a block diagram of the micro-architecture for a processor200that includes logic circuits to perform identification and management of unsafe optimizations in accordance with one embodiment of the present disclosure. In some embodiments, an instruction in accordance with one embodiment can be implemented to operate on data elements having sizes of byte, word, doubleword, quadword, etc., as well as datatypes, such as single and double precision integer and floating point datatypes. In one embodiment the in-order front end201is the part of the processor200that fetches instructions to be executed and prepares them to be used later in the processor pipeline. The front end201may include several units. In one embodiment, the instruction prefetcher226fetches instructions from memory and feeds them to an instruction decoder228which in turn decodes or interprets them. For example, in one embodiment, the decoder decodes a received instruction into one or more operations called “micro-instructions” or “micro-operations” (also called micro op or uops) that the machine can execute. In other embodiments, the decoder parses the instruction into an opcode and corresponding data and control fields that are used by the micro-architecture to perform operations in accordance with one embodiment. In one embodiment, the trace cache230takes decoded uops and assembles them into program ordered sequences or traces in the uop queue234for execution. When the trace cache230encounters a complex instruction, the microcode ROM232provides the uops needed to complete the operation.

Some instructions are converted into a single micro-op, whereas others need several micro-ops to complete the full operation. In one embodiment, if more than four micro-ops are needed to complete a instruction, the decoder228accesses the microcode ROM232to do the instruction. For one embodiment, an instruction can be decoded into a small number of micro ops for processing at the instruction decoder228. In another embodiment, an instruction can be stored within the microcode ROM232should a number of micro-ops be needed to accomplish the operation. The trace cache230refers to a entry point programmable logic array (PLA) to determine a correct micro-instruction pointer for reading the micro-code sequences to complete one or more instructions in accordance with one embodiment from the micro-code ROM232. After the microcode ROM232finishes sequencing micro-ops for an instruction, the front end201of the machine resumes fetching micro-ops from the trace cache230.

The out-of-order execution engine203is where the instructions are prepared for execution. The out-of-order execution logic has a number of buffers to smooth out and re-order the flow of instructions to optimize performance as they go down the pipeline and get scheduled for execution. The allocator logic allocates the machine buffers and resources that each uop needs in order to execute. The register renaming logic renames logic registers onto entries in a register file. The allocator also allocates an entry for each uop in one of the two uop queues, one for memory operations and one for non-memory operations, in front of the instruction schedulers: memory scheduler, fast scheduler202, slow/general floating point scheduler204, and simple floating point scheduler206. The uop schedulers202,204,206, determine when a uop is ready to execute based on the readiness of their dependent input register operand sources and the availability of the execution resources the uops need to complete their operation. The fast scheduler202of one embodiment can schedule on each half of the main clock cycle while the other schedulers can only schedule once per main processor clock cycle. The schedulers arbitrate for the dispatch ports to schedule uops for execution.

Register files208,210, sit between the schedulers202,204,216, and the execution units212,214,216,218,220,222,224in the execution block211. There is a separate register file208,210, for integer and floating point operations, respectively. Each register file208,210, of one embodiment also includes a bypass network that can bypass or forward just completed results that have not yet been written into the register file to new dependent uops. The integer register file208and the floating point register file210are also capable of communicating data with the other. For one embodiment, the integer register file208is split into two separate register files, one register file for the low order 32 bits of data and a second register file for the high order 32 bits of data. The floating point register file210of one embodiment has 128 bit wide entries because floating point instructions typically have operands from 64 to 128 bits in width.

The execution block211contains the execution units212,214,216,218,220,222,224, where the instructions are actually executed. This section includes the register files208,210, that store the integer and floating point data operand values that the micro-instructions need to execute. The processor200of one embodiment is comprised of a number of execution units: address generation unit (AGUT)212. AGU214, fast ALIT216, fast ALU218, slow ALIT220, floating point ALU222, floating point move unit224. For one embodiment, the floating point execution blocks222,224, execute floating point, MMX, SIMD, and SSE, or other operations. The floating point ALU222of one embodiment includes a 64 bit by 64 bit floating point divider to execute divide, square root, and remainder micro-ops. For embodiments of the present invention, instructions involving a floating point value may be handled with the floating point hardware. In one embodiment, the ALU operations go to the high-speed ALU execution units216,218. The fast ALUs216,218, of one embodiment can execute fast operations with an effective latency of half a clock cycle. For one embodiment, most complex integer operations go to the slow ALU220as the slow ALU220includes integer execution hardware for long latency type of operations, such as a multiplier, shifts, flag logic, and branch processing. Memory load/store operations are executed by the AGUs212,214. For one embodiment, the integer ALUs216,218,220, are described in the context of performing integer operations on 64 bit data operands. In alternative embodiments, the ALUs216,218,220, can be implemented to support a variety of data bits including16,32,128,256, etc. Similarly, the floating point units222,224, can be implemented to support a range of operands having bits of various widths. For one embodiment, the floating point units222,224, can operate on 128 bits wide packed data operands in conjunction with SIMD and multimedia instructions.

In one embodiment, the uops schedulers202,204,206, dispatch dependent operations before the parent load has finished executing. As uops are speculatively scheduled and executed in processor200, the processor200also includes logic to handle memory misses. If a data load misses in the data cache, there can be dependent operations in flight in the pipeline that have left the scheduler with temporarily incorrect data. A replay mechanism tracks and re-executes instructions that use incorrect data. Only the dependent operations need to be replayed and the independent ones are allowed to complete. The schedulers and replay mechanism of one embodiment of a processor are also designed to catch instruction sequences for text string comparison operations.

In one embodiment, processor200also includes logic to implement identification and management of unsafe optimizations according to embodiments of the invention. For example, processor200may include logic to execute an MRE, such as MRE150described with respect toFIG. 1, where the MRE implements identification and management of unsafe optimizations. In other embodiments, and as discussed further below with respect toFIG. 4, processing device200may include hardware circuitry to provide a last taken branch (e.g., a register to store this information), a global misprediction cache, a global misprediction counter, and a timer, which operate in conjunction with the MRE to provide identification and management of unsafe optimizations according to at least one embodiment.

The term “registers” may refer to the on-board processor storage locations that are used as part of instructions to identify operands. In other words, registers may be those that are usable from the outside of the processor (from a programmer's perspective). However, the registers of an embodiment should not be limited in meaning to a particular type of circuit. Rather, a register of an embodiment is capable of storing and providing data, and performing the functions described herein. The registers described herein can be implemented by circuitry within a processor using any number of different techniques, such as dedicated physical registers, dynamically allocated physical registers using register renaming, combinations of dedicated and dynamically allocated physical registers, etc. In one embodiment, integer registers store thirty-two bit integer data. A register file of one embodiment also contains eight multimedia SIMD registers for packed data. For the discussions below, the registers are understood to be data registers designed to hold packed data, such as 64 bits wide MMXTM registers (also referred to as ‘mm’ registers in some instances) in microprocessors enabled with MMX technology from Intel Corporation of Santa Clara, Calif. These MMX registers, available in both integer and floating point forms, can operate with packed data elements that accompany SIMD and SSE instructions. Similarly, 128 bits wide XMM registers relating to SSE2, SSE3, SSE4, or beyond (referred to generically as “SSEx”) technology can also be used to hold such packed data operands. In one embodiment, in storing packed data and integer data, the registers do not need to differentiate between the two data types. In one embodiment, integer and floating point are either contained in the same register file or different register files. Furthermore, in one embodiment, floating point and integer data may be stored in different registers or the same registers.

FIG. 3Ais a block diagram illustrating an in-order pipeline and a register renaming stage, out-of-order issue/execution pipeline in which at least one embodiment of the disclosure may be used. The solid lined boxes inFIG. 3Aillustrate an in-order pipeline, while the dashed lined boxes illustrates an register renaming, out-of-order issue/execution pipeline. InFIG. 3A, a processor pipeline300includes a fetch stage302, a length decode stage304, a decode stage306, an allocation stage308, a renaming stage310, a scheduling (also known as a dispatch or issue) stage312, a register read/memory read stage314, an execute stage316, a write back/memory write stage318, an exception handling stage322, and a commit stage324.

FIG. 3Billustrates a block diagram of a micro-architecture for a processor390that implements identification and management of unsafe optimizations in which one embodiment of the disclosure may be used. Specifically, processor390depicts an in-order architecture core and a register renaming logic, out-of-order issue/execution logic to be included in a processor according to at least one embodiment of the invention. In one embodiment, any of CPUs135ofFIG. 1may be implemented as processor390.

Processor390includes a front end unit330coupled to an execution engine unit350, and both are coupled to a memory unit370. The processor390may include a reduced instruction set computing (RISC) core, a complex instruction set computing (CISC) core, a very long instruction word (VLIW) core, or a hybrid or alternative core type. As yet another option, processor390may include a special-purpose core, such as, for example, a network or communication core, compression engine, graphics core, or the like. In one embodiment, processor390may be a multi-core processor or may part of a multi-processor system, such as system600described further below with respect toFIG. 6.

The front end unit330includes a branch prediction unit332coupled to an instruction cache unit334, which is coupled to an instruction translation lookaside buffer (TLB)336, which is coupled to an instruction fetch unit338, which is coupled to a decode unit340. The decode unit340(also known as a decoder) may decode instructions, and generate as an output one or more micro-operations, micro-code entry points, microinstructions, other instructions, or other control signals, which are decoded from, or which otherwise reflect, or are derived from, the original instructions. The decoder may be implemented using various different mechanisms. Examples of suitable mechanisms include, but are not limited to, look-up tables, hardware implementations, programmable logic arrays (PLAs), microcode read only memories (ROMs), etc. The instruction cache unit334is further coupled to the memory unit370. The decode unit340is coupled to a rename/allocator unit352in the execution engine unit350.

The execution engine unit350includes the rename/allocator unit352coupled to a retirement unit354and a set of one or more scheduler unit(s)356. The scheduler unit(s)356represents any number of different schedulers, including reservations stations (RS), central instruction window, etc. The scheduler unit(s)356is coupled to the physical register file(s) unit(s)358. Each of the physical register file(s) units358represents one or more physical register files, different ones of which store one or more different data types, such as scalar integer, scalar floating point, packed integer, packed floating point, vector integer, vector floating point, etc., status (e.g., an instruction pointer that is the address of the next instruction to be executed), etc. The physical register file(s) unit(s)358is overlapped by the retirement unit354to illustrate various ways in which register renaming and out-of-order execution may be implemented (e.g., using a reorder buffer(s) and a retirement register file(s), using a future file(s), a history buffer(s), and a retirement register file(s) using a register maps and a pool of registers; etc.).

Generally, the architectural registers are visible from the outside of the processor or from a programmer's perspective. The registers are not limited to any known particular type of circuit. Various different types of registers are suitable as long as they are capable of storing and providing data as described herein. Examples of suitable registers include, but are not limited to, dedicated physical registers, dynamically allocated physical registers using register renaming, combinations of dedicated and dynamically allocated physical registers, etc. The retirement unit354and the physical register file(s) unit(s)358are coupled to the execution cluster(s)360. The execution cluster(s)360includes a set of one or more execution units362and a set of one or more memory access units364. The execution units362may perform various operations (e.g., shifts, addition, subtraction, multiplication) and operate on various types of data (e.g., scalar floating point, packed integer, packed floating point, vector integer, vector floating point).

While some embodiments may include a number of execution units dedicated to specific functions or sets of functions, other embodiments may include only one execution unit or multiple execution units that all perform all functions. The scheduler unit(s)356, physical register file(s) unit(s)358, and execution cluster(s)360are shown as being possibly plural because certain embodiments create separate pipelines for certain types of data/operations (e.g., a scalar integer pipeline, a scalar floating point/packed integer/packed floating point/vector integer/vector floating point pipeline, and/or a memory access pipeline that each have their own scheduler unit, physical register file(s) unit, and/or execution cluster—and in the case of a separate memory access pipeline, certain embodiments are implemented in which only the execution cluster of this pipeline has the memory access unit(s)364). It should also be understood that where separate pipelines are used, one or more of these pipelines may be out-of-order issue/execution and the rest in-order.

The set of memory access units364is coupled to the memory unit370, which may include a data TLB unit372, a data cache unit (DCU) (i.e., L1 cache)374, and a level 2 (L2) cache unit376, to name a few examples. In some embodiments DCU374is also known as a first level data cache (L1 cache). The DCU374may handle multiple outstanding cache misses and continue to service incoming stores and loads. It also supports maintaining cache coherency. The data TLB unit372is a cache used to improve virtual address translation speed by mapping virtual and physical address spaces. In one exemplary embodiment, the memory access units364may include a load unit, a store address unit, and a store data unit, each of which is coupled to the data TLB unit372in the memory unit370. The L2 cache unit376may be coupled to one or more other levels of cache and eventually to a main memory.

The processor390may support one or more instructions sets (e.g., the x86 instruction set (with some extensions that have been added with newer versions); the MIPS instruction set of MIPS Technologies of Sunnyvale, Calif.; the ARM instruction set (with optional additional extensions such as NEON) of ARM Holdings of Sunnyvale, Calif.).

In one embodiment, processor390also includes logic to implement identification and management of unsafe optimizations according to embodiments of the invention. For example, processor390may include logic to execute an MRE, such as MRE150described with respect toFIG. 1, where the MRE implements identification and management of unsafe optimizations. In other embodiments, and as discussed further below with respect toFIG. 4, processing device390may include hardware circuitry to provide a last taken branch (e.g., a register to store this information), a global misprediction cache, a global misprediction counter, and a timer, which operate in conjunction with the MRE to provide identification and management of unsafe optimizations according to at least one embodiment.

FIG. 4illustrates a block diagram of a system400for identification and management of unsafe optimizations in which at least one embodiment of the disclosure may be used. System100includes an MRE410, a global misprediction counter420, a global misprediction cache450, a timer460, and a last taken branch470. In embodiments of the disclosure, the components410,420,450,460,470may be implemented fully in software, hardware, firmware, or some combination of the above. For example, in some embodiments, components420,450,460, and470are implemented in hardware circuitry (e.g., in processor130ofFIG. 1, processor200ofFIG. 2, and/or processor390ofFIG. 3B) that is communicably coupled to MRE410.

In one embodiment, MRE410is the same as MRE150ofFIG. 1. In some embodiments, although not specifically illustrated, MRE410includes the misprediction manager175discussed with respect toFIG. 1to perform aspects of the identification and management of unsafe optimization described herein. MRE410generates optimized code430that may include an unsafe optimization. As previously discussed, an unsafe optimization occurs when a misprediction (e.g., wrong branch prediction, etc.) arises during runtime of the optimized code. MRE410also generates backup code440when such an unsafe optimization occurs.

The MRE410may manage the global misprediction counter (GMC)420and the global misprediction cache (MC)450. Each entry in the MC450contains the address (tag) of the failing optimization code region, a pointer to backup code for that code region, and a local misprediction counter (LMC) associated with the code region. In some embodiments, the MC450may include additional information regarding a failing optimization code region and is not limited to the data described above.

System400also includes a timer460that periodically generates interrupts to the MRE410to clear the counters (i.e., GMC420and LMCs). The system may also include storage for a last taken branch470of the executing code. In one embodiment, the storage for the last taken branch470may comprise a hardware register.

In embodiments of the invention, the components of system400operate together to identify and selectively manage unsafe optimizations. When a misprediction in optimized code430generated by the MRE410occurs, the MRE410is notified (e.g., by the processor executing the optimized code). When the notification is received, the MRE410increments the GMC420. The GMC420tracks an overall number of mispredictions occurring with respect to optimized code430generated by the MRE410.

On the first misprediction of any portion of the optimized code430, the MRE410generates a backup version440of the code region that failed, where the backup version440does not include the optimization. In addition, the MRE410allocates a new entry in the MC450for the portion of the code having the failing optimization. The MRE410may identify the portion of optimized code that failed by reading storage for a last taken branch470that, in one embodiment, is exposed as a control register that can be read.

The MRE410then writes data to the newly-allocated entry in the MC450, where the data includes the address of the failing optimization (tag), a pointer to the backup code440, and the LMC for that portion of optimized code430associated with the unsafe optimization. After handling the first miss for a portion of optimized code, execution continues for the portion of code having the unsafe optimization by utilizing the generated backup code440. However, as soon as the program code is to be executed again, the optimized code430is dispatched again and includes the optimized version of the portion of code having the unsafe optimization.

In further embodiments, any time a misprediction occurs in the optimized code430again, the GMC420is increased again. Each time the GMC420is increased, the MRE410determines whether a value of the GMC420is below a global pessimization threshold (GPT). In one embodiment, a system administrator may set the value of the GPT, and this value may be stored in memory associated with the MRE410. If the value of the GMC420is below the GPT, then the MRE410checks the MC450to determine if an entry exists for the portion of code having the unsafe optimization. If there is a hit, then the pointer to the backup code440may be used to issue the backup code440for continuing execution purposes. If there is not a hit, then a new entry is allocated in the MC450for the portion of code having the unsafe optimization per the above description.

In some embodiments, it may happen that a misprediction entry is evicted from the MC450. In this case, the backup code440associated with the particular entry is not discarded, but instead is recorded in memory. When a misprediction happens and there is no corresponding entry in the MC450, the system400may then perform a lookup for these stored backup versions before creating a new version of backup code440.

In one embodiment, if the GMC is above the GPT, then the MC450is checked according to the above. However, in this case, in addition to dispatching the backup code440, the LMC of the entry is increased by one, while the GMC420is reset to zero. Whenever the LMC of an MC450entry is increased, the value of the LMC is compared against a local pessimization threshold (LPT). In one embodiment, a system administrator may set the value of GPT, and this value may be stored in memory associated with the MRE410. The LPT may be the same for each entry in the MC450. If the LPT is not exceeded, then the backup code440is issued as per the above.

On the other hand, if the LPT is exceeded, then the optimized code430is determined to be causing too many mispredictions and is to be recompiled. When this occurs, the MRE410generates a new version of the optimized code. In one embodiment, this new version of optimized code disables any optimizations to the portion of code that exceeded the LPT. As a result, going forward, the optimized version of the program code is run without optimizations for that particular portion/region of code that had become problematic.

Lastly, tinier460periodically generates interrupts to the MRE410. On these interrupts, the MRE410sets to zero the GMC420and all of the LMCs in MC450. As a result, timer460facilitates the MRE410in speculating mispredictions over set periods of time. The MRE410may use the GMC420and LMCs to sample which optimizations are significantly harming performance. If an optimized code region does not fail very often and most of time results in performance gains, then the MRE410keeps this optimized code as a result of the resets of the counters (GMC420and LMCs) caused by timer460.

FIG. 5is a flow diagram illustrating a method500for identifying and managing unsafe optimizations according to an embodiment of the disclosure. Method500may be performed by processing logic that may comprise hardware (e.g., circuitry, dedicated logic, programmable logic, microcode, etc.), software (such as instructions run on a processing device), firmware, or a combination thereof. In one embodiment, method500is performed by MRE150ofFIG. 1.

Method500begins at block505where optimized code for an application is provided to a processor for execution. In one embodiment, an MRE provides this optimized code to the processor. At block510, notification of a misprediction during runtime of the optimized code is received. In one embodiment, the processor provides this notification to the MRE. Then, at block515, a count of a global misprediction counter is increased to reflect the occurrence of the misprediction by the optimized code. At block520, a failing code region of the optimized code is identified via a read of a last branch taken data store. In one embodiment, the last branch taken data store is a hardware control register.

At decision block525, it is determined whether the identified code region has a corresponding entry in an MC associated with the MRE. In one embodiment, the MC is implemented as a software cache of the MRE. In other embodiments, the MC is implemented in hardware separate from, but accessible by, the MRE. If an entry if the MC corresponding to the identified code region does exist, then method500proceeds to decision block530to determine whether the GMC exceeds a GMT. If so, then method500proceeds to block540to increase an LMC of the corresponding entry and reset the GMC to zero. Then, at decision block545, after the LMC is increased, it is compared to an LMT to determine if it exceeds the LMT. If so, then at block550, the code optimization is identified as failing due to excessive failure of optimization at the code region. In addition, a new optimized code version is created via recompilation, with the identified code region disabled for purposes of optimization.

Returning to decision blocks530and545, if either of the GMC or the LMC does not exceed the GMT or the LMT, respectively, then method500proceeds to block565where backup code for the identified code region is dispatched. In one embodiment, the backup code is identified via a pointer kept in the entry of the MC associated with the identified code region.

Returning to decision block525, if the identified code region does not have a corresponding entry in the MC, then method500proceeds to decision block535where it is determined whether a version of backup code for the identified code region exists. In one embodiment, memory is checked for the backup code for the identified code region. If the backup code does not exist, then method500continues to block555to generate the back up code and then proceed to block560. On the other hand, if the backup code does exist, then method500proceeds directly to block560. At block560, an entry in the MC is allocated for the identified code region. In one embodiment, the entry in the MC includes a tag identifying the code region, a pointer to the backup code associated with the code region, and an LMC associated with the code region. Then, method500continues to block565, where, as discussed above, the backup code for the identified code region is dispatched.

Referring now toFIG. 6, shown is a block diagram of a system600in which an embodiment of the invention may be used. As shown inFIG. 6, multiprocessor system600is a point-to-point interconnect system, and includes a first processor670and a second processor680coupled via a point-to-point interconnect650. While shown with only two processors670,680, it is to be understood that the scope of embodiments of the invention is not so limited. In other embodiments, one or more additional processors may be present in a given processor. In one embodiment, the multiprocessor system600may implement an MRE for identifying and managing unsafe optimizations as described herein.

Processors670and680are shown including integrated memory controller units672and682, respectively. Processor670also includes as part of its bus controller units point-to-point (P-P) interfaces676and678; similarly, second processor680includes P-P interfaces686and688. Processors670,680may exchange information via a point-to-point (P-P) interface650using P-P interface circuits678,688. As shown inFIG. 6, IMCs672and682couple the processors to respective memories, namely a memory632and a memory634, which may be portions of main memory locally attached to the respective processors.

Processors670,680may each exchange information with a chipset690via individual P-P interfaces652,654using point to point interface circuits676,694,686,698. Chipset690may also exchange information with a high-performance graphics circuit638via a high-performance graphics interface639.

Chipset690may be coupled to a first bus616via an interface696. In one embodiment, first bus616may be a Peripheral Component Interconnect (PCI) bus, or a bus such as a PCI Express bus or another third generation I/O interconnect bus, although the scope of the present invention is not so limited.

As shown inFIG. 6, various I/O devices614may be coupled to first bus616, along with a bus bridge618which couples first bus616to a second bus620. In one embodiment, second bus620may be a low pin count (LPC) bus. Various devices may be coupled to second bus620including, for example, a keyboard and/or mouse622, communication devices627and a storage unit628such as a disk drive or other mass storage device which may include instructions/code and data630, in one embodiment. Further, an audio I/O624may be coupled to second bus620. Note that other architectures are possible. For example, instead of the point-to-point architecture ofFIG. 6, a system may implement a multi-drop bus or other such architecture.

Processing device702represents one or more general-purpose processing devices such as a microprocessor, central processing unit, or the like. More particularly, the processing device may be complex instruction set computing (CISC) microprocessor, reduced instruction set computer (RISC) microprocessor, very long instruction word (VLIW) microprocessor, or processor implementing other instruction sets, or processors implementing a combination of instruction sets. Processing device702may also be one or more special-purpose processing devices such as an application specific integrated circuit (ASIC), a field programmable gate array (FPGA), a digital signal processor (DSP), network processor, or the like. In one embodiment, processing device702may include one or processing cores. The processing device702is configured to execute the processing logic726for performing the operations and steps discussed herein. In one embodiment, processing device702is the same as multi-core processor130described with respect toFIG. 1that implements an MRE for identifying and managing unsafe optimizations as described herein with embodiments of the disclosure.

The computer system700may further include a network interface device708communicably coupled to a network720. The computer system700also may include a video display unit710(e.g., a liquid crystal display (LCD) or a cathode ray tube (CRT)), an alphanumeric input device712(e.g., a keyboard), a cursor control device714(e.g., a mouse), and a signal generation device716(e.g., a speaker). Furthermore, computer system700may include a graphics processing unit722, a video processing unit728, and an audio processing unit732.

The data storage device718may include a machine-accessible storage medium724on which is stored software726implementing any one or more of the methodologies of functions described herein, such as implementing an MRE for identifying and managing unsafe optimizations with restricted entries as described above. The software726may also reside, completely or at least partially, within the main memory704as instructions726and/or within the processing device702as processing logic726during execution thereof by the computer system700; the main memory704and the processing device702also constituting machine-accessible storage media.

The machine-readable storage medium724may also be used to store instructions726implementing an MRE that identifies and manages unsafe optimizations, such as described with respect to MRE150inFIG. 1, and/or a software library containing methods that call the above applications. While the machine-accessible storage medium728is shown in an example embodiment to be a single medium, the term “machine-accessible storage medium” should be taken to include a single medium or multiple media (e.g. a centralized or distributed database, and/or associated caches and servers) that store the one or more sets of instructions. The term “machine-accessible storage medium” shall also be taken to include any medium that is capable of storing, encoding or carrying a set of instruction for execution by the machine and that cause the machine to perform any one or more of the methodologies of the present invention. The term “machine-accessible storage medium” shall accordingly be taken to include, but not be limited to, solid-state memories, and optical and magnetic media.

The following examples pertain to further embodiments. Example 1 is a method for identifying and managing unsafe optimizations comprising receiving, by a managed runtime environment (MRE) executed by a processing device, a notice of misprediction of optimized code, the misprediction occurring during a runtime of the optimized code, determining, by the MRE, whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, compiling, by the MRE, native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized. In Example 2, the subject matter of Example 1 can optionally include wherein the misprediction comprises an incorrect branch speculation of the optimized code. In Example 3, the subject matter of any one of Examples 1-2 can optionally include increasing a global misprediction counter (GMC) associated with the optimized code when the notice of misprediction is received.

In Example 4, the subject matter of any one of Examples 1-3 can optionally include when the GMC exceeds a global misprediction threshold (GMT) value, increasing the LMC, and performing the determining whether the LMC exceeds the LMT value. In Example 5, the subject matter of any one of Examples 1-4 can optionally include resetting the GMC when the LMC is increased. In Example 6, the subject matter of any one of Examples 1-5 can optionally include receiving an indication of an expiration of a timer, and resetting the GMC and the LMC to zero in response to the indication of the expiration of the timer.

In Example 7, the subject matter of any one of Examples 1-6 can optionally include resetting the LMC to zero subsequent to the compiling. In Example 8, the subject matter of any one of Examples 1-7 can optionally include identifying the code region via a last branch taken register, and determining whether an entry exists in a global misprediction cache (MC) that is associated with the identified code region.

In Example 9, the subject matter of any one of Examples 1-8 can optionally include when an entry does exists in the MC for the identified code region and when the GMC does not exceed the GMT, dispatching a backup version of the code region for execution by a processing device. In Example 10, the subject matter of any one of Examples 1-9 can optionally include wherein the backup version comprises an unoptimized version of the native code of the optimized code.

In Example 11, the subject matter of any one of Examples 1-10 can optionally include when an entry does not exist in the MC, determining whether the backup version exists in memory, when the backup version does not exist, generating the backup version, allocating an entry in the MC for the identified code region; and dispatching the backup version of the code region for execution by a processing device. In Example 12, the subject matter of any one of Examples 1-11 can optionally include wherein the entry in the MC comprises a tag identifying the code region, a pointer to the backup version of the code, and the LMC for the code region.

Example 13 is an apparatus for identifying and managing unsafe optimizations comprising a plurality of registers and a main processing unit to execute a managed runtime environment (MRE) that generates an optimized version of code, execute the optimized version of code, and send notification of occurrence of a misprediction during the execution of the optimized version of the code, the notice sent to the MRE. In Example 13, the MRE is to determine whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, compile native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized.

In Example 14, the subject matter of Example 13 can optionally include wherein the misprediction comprises an incorrect branch speculation of the optimized code. In Example 15, the subject matter of any one of Examples 13-14 can optionally include wherein the MRE further to increase a global misprediction counter (GMC) associated with the optimized code when the notice of misprediction is received. In Example 16, the subject matter of any one of Examples 13-15 can optionally include wherein the MRE further to, when the GMC exceeds a global misprediction threshold (GMT) value, increase the LMC, and perform the determining whether the LMC exceeds the LMT value.

In Example 17, the subject matter of any one of Examples 13-16 can optionally include wherein the MRE further to reset the GMC when the LMC is increased. In Example 18, the subject matter of any one of Examples 13-17 can optionally include wherein the MRE further to receive an indication of an expiration of a timer, and reset the GMC and the LMC to zero in response to the indication of the expiration of the timer. In Example 19, the subject matter of any one of Examples 13-18 can optionally include wherein the MRE further to reset the LMC to zero subsequent to the compiling. In Example 20, the subject matter of any one of Examples 13-19 can optionally include wherein the MRE further to identify the code region via a last branch taken register, and determine whether an entry exists in a global misprediction cache (MC) that is associated with the identified code region.

In Example 21, the subject matter of any one of Examples 13-20 can optionally include wherein the MRE further to, when an entry does exists in the MC for the identified code region and when the GMC does not exceed the GMT, dispatch a backup version of the code region for execution by a processing device. In Example 22, the subject matter of any one of Examples 13-21 can optionally include wherein the MRE further to, when an entry does not exist in the MC determine whether the backup version exists in memory, when the backup version does not exist, generate the backup version, allocate an entry in the MC for the identified code region, and dispatch the backup version of the code region for execution by a processing device. All optional features of the apparatus described above may also be implemented with respect to the method or process described herein.

Example 23 is a system for identifying and managing unsafe optimizations. In Example 23, the system includes a processing device, a memory communicably coupled to the processing device, the memory to store instructions to execute a managed runtime environment (MRE), and the MRE executable from the memory by the processing device. In Example 23, the MRE is to receive, from the processing device, a notice of misprediction of optimized code, the misprediction occurring during a runtime of the optimized code and comprising an incorrect branch speculation of the optimized code, determine whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, compile native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized.

In Example 24, the subject matter of Example 23 can optionally include wherein the MRE further to increase a global misprediction counter (GMC) associated with the optimized code when the notice of misprediction is received. In Example 25, the subject matter of any one of Examples 23-24 can optionally include wherein the MRE further to when the GMC exceeds a global misprediction threshold (GMT) value, increase the LMC, and perform the determining whether the LMC exceeds the LMT value. In Example 26, the subject matter of any one of Examples 23-25 can optionally include wherein the MRE further to reset the GMC when the LMC is increased.

In Example 27, the subject matter of any one of Examples 23-26 can optionally include wherein the MRE further to receive an indication of an expiration of a timer, and reset the GMC and the LMC to zero in response to the indication of the expiration of the timer. In Example 28, the subject matter of any one of Examples 23-27 can optionally include wherein the MRE further to reset the LMC to zero subsequent to the compiling. In Example 29, the subject matter of any one of Examples 23-28 can optionally include wherein the MRE further to identify the code region via a last branch taken register, and determine whether an entry exists in a global misprediction cache (MC) that is associated with the identified code region, wherein the entry in the MC comprises a tag identifying the code region, a pointer to the backup version of the code, and the LMC for the code region.

In Example 30, the subject matter of any one of Examples 23-29 can optionally include wherein the MRE further to, when an entry does exists in the MC for the identified code region and when the GMC does not exceed the GMT, dispatch a backup version of the code region for execution by a processing device. In Example 31, the subject matter of any one of Examples 23-30 can optionally include wherein the backup version comprises an unoptimized version of the native code of the optimized code. In Example 32, the subject matter of any one of Examples 23-31 can optionally include wherein the MRE further to, when an entry does not exist in the MC, determine whether the backup version exists in memory, when the backup version does not exist, generate the backup version, allocate an entry in the MC for the identified code region, and dispatch the backup version of the code region for execution by a processing device. All optional features of the system described above may also be implemented with respect to the method or process described herein.

Example 33 is non-transitory computer-readable medium for identifying and managing unsafe optimizations comprising means. In Example 33, the non-transitory machine-readable medium includes data that, when accessed by a processing device, cause the processing device to perform operations comprising receiving, by a managed runtime environment (MRE) executed by the processing device, a notice of misprediction of optimized code, the misprediction occurring during a runtime of the optimized code, determining, by the MRE, whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, compiling, by the MRE, native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized.

In Example 34, the subject matter of Example 33 can optionally include wherein the misprediction comprises an incorrect branch speculation of the optimized code. In Example 35, the subject matter of any one of Examples 33-34 can optionally include increasing a global misprediction counter (GMC) associated with the optimized code when the notice of misprediction is received, and when the GMC exceeds a global misprediction threshold (GMT) value, increasing the LMC, and performing the determining whether the LMC exceeds the LMT value.

In Example 36, the subject matter of any one of Examples 33-35 can optionally include resetting the GMC when the LMC is increased. In Example 37, the subject matter of any one of Examples 33-36 can optionally include receiving an indication of an expiration of a timer, and resetting the GMC and the LMC to zero in response to the indication of the expiration of the timer. In Example 38, the subject matter of any one of Examples 33-37 can optionally include resetting the LMC to zero subsequent to the compiling.

In Example 39, the subject matter of any one of Examples 33-38 can optionally include identifying the code region via a last branch taken register, and determining whether an entry exists in a global misprediction cache (MC) that is associated with the identified code region. In Example 40, the subject matter of any one of Examples 33-39 can optionally include when an entry does exists in the MC for the identified code region and when the GMC does not exceed the GMT, dispatching a backup version of the code region for execution by a processing device, wherein the backup version comprises an unoptimized version of the native code of the optimized code. In Example 41, the subject matter of any one of Examples 33-40 can optionally include when an entry does not exist in the MC, determining whether the backup version exists in memory, when the backup version does not exist, generating the backup version, allocating an entry in the MC for the identified code region, and dispatching the backup version of the code region for execution by a processing device.

Example 42 is an apparatus for identifying and managing unsafe optimizations comprising means for receiving a notice of misprediction of optimized code, the misprediction occurring during a runtime of the optimized code, means for determining, whether a local misprediction counter (LMC) associated with a code region of the optimized code causing the misprediction exceeds a local misprediction threshold (LMT) value, and when the LMC exceeds the LMT value, means for compiling native code of the optimized code to generate a new version of the optimized code, wherein the code region in the new version of the optimized code is not optimized. In Example 43, the subject matter of Example 42 can optionally include the apparatus further configured to perform the method of any one of the claims2to12.

In Example 44, the subject matter of Example 23 further configured to perform the method of any one of the claims2to12.

Example 45 is at least one machine readable medium comprising a plurality of instructions that in response to being executed on a computing device, cause the computing device to carry out a method according to any one of Examples 1-12. Example 46 is an apparatus for identifying and managing unsafe optimizations, configured to perform the method of any one of Examples 1-12. Example 47 is an apparatus comprising means for performing the method of any one of Examples 1-12. Specifics in the Examples may be used anywhere in one or more embodiments.