Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm] Add an InstCount observation space. #159

Merged
merged 3 commits into from
Mar 30, 2021

Conversation

ChrisCummins
Copy link
Contributor

@ChrisCummins ChrisCummins commented Mar 25, 2021

This adds new observation spaces that expose the -instcount pass
values. The -instcount pass counts the number of instructions of each
type in a program, along with the total number of instructions, total
number of blocks, and total number of functions.

There are four new observation spaces: InstCount, which returns the
feature vector as a numpy array, InstCountDict, which returns the
values as a dictionary of named features, and InstCountNorm and
InstCountNormDict, which are the same as above but the counts are
instead normalized to the total number of instructions in the program.

Example usage:

>>> import gym
>>> import compiler_gym
>>> env = gym.make("llvm-v0")
>>> env.observation_space = "InstCountDict"
>>> env.reset("cBench-v0/crc32")
{'TotalInstsCount': 196, 'TotalBlocksCount': 29,
'TotalFuncsCount': 13, 'RetCount': 5, 'BrCount': 24,
'SwitchCount': 0, 'IndirectBrCount': 0, 'InvokeCount': 0,
'ResumeCount': 0, 'UnreachableCount': 0, 'CleanupRetCount': 0,
'CatchRetCount': 0, 'CatchSwitchCount': 0, 'CallBrCount': 0,
'FNegCount': 0, 'AddCount': 5, 'FAddCount': 0, 'SubCount': 0,
'FSubCount': 0, 'MulCount': 0, 'FMulCount': 0, 'UDivCount': 0,
'SDivCount': 0, 'FDivCount': 0, 'URemCount': 0, 'SRemCount': 0,
'FRemCount': 0, 'ShlCount': 0, 'LShrCount': 3, 'AShrCount': 0,
'AndCount': 3, 'OrCount': 1, 'XorCount': 8, 'AllocaCount': 24,
'LoadCount': 51, 'StoreCount': 38, 'GetElementPtrCount': 5,
'FenceCount': 0, 'AtomicCmpXchgCount': 0, 'AtomicRMWCount': 0,
'TruncCount': 1, 'ZExtCount': 5, 'SExtCount': 0, 'FPToUICount': 0,
'FPToSICount': 0, 'UIToFPCount': 0, 'SIToFPCount': 0,
'FPTruncCount': 0, 'FPExtCount': 0, 'PtrToIntCount': 0,
'IntToPtrCount': 0, 'BitCastCount': 0, 'AddrSpaceCastCount': 0,
'CleanupPadCount': 0, 'CatchPadCount': 0, 'ICmpCount': 10,
'FCmpCount': 0, 'PHICount': 0, 'CallCount': 13, 'SelectCount': 0,
'UserOp1Count': 0, 'UserOp2Count': 0, 'VAArgCount': 0,
'ExtractElementCount': 0, 'InsertElementCount': 0,
'ShuffleVectorCount': 0, 'ExtractValueCount': 0,
'InsertValueCount': 0, 'LandingPadCount': 0, 'FreezeCount': 0}

The InstCount observation spaces are quick to compute and
lightweight. They have similar computational complexity as Autophase.

Fixes #149.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 25, 2021
This adds new observation spaces that expose the -instcount pass
values. The -instcount pass counts the number of instructions of each
type in a program, along with the total number of instructions, total
number of blocks, and total number of functions.

There are four new observation spaces: `InstCount`, which returns the
feature vector as a numpy array, `InstCountDict`, which returns the
values as a dictionary of named features, and `InstCountNorm` and
`InstCountNormDict`, which are the same as above but the counts are
instead normalized to the total number of instructions in the program.

Example usage:

    >>> import gym
    >>> import compiler_gym
    >>> env = gym.make("llvm-v0")
    >>> env.observation_space = "InstCountDict"
    >>> env.reset("cBench-v0/crc32")
    {'TotalInstsCount': 196, 'TotalBlocksCount': 29,
    'TotalFuncsCount': 13, 'RetCount': 5, 'BrCount': 24,
    'SwitchCount': 0, 'IndirectBrCount': 0, 'InvokeCount': 0,
    'ResumeCount': 0, 'UnreachableCount': 0, 'CleanupRetCount': 0,
    'CatchRetCount': 0, 'CatchSwitchCount': 0, 'CallBrCount': 0,
    'FNegCount': 0, 'AddCount': 5, 'FAddCount': 0, 'SubCount': 0,
    'FSubCount': 0, 'MulCount': 0, 'FMulCount': 0, 'UDivCount': 0,
    'SDivCount': 0, 'FDivCount': 0, 'URemCount': 0, 'SRemCount': 0,
    'FRemCount': 0, 'ShlCount': 0, 'LShrCount': 3, 'AShrCount': 0,
    'AndCount': 3, 'OrCount': 1, 'XorCount': 8, 'AllocaCount': 24,
    'LoadCount': 51, 'StoreCount': 38, 'GetElementPtrCount': 5,
    'FenceCount': 0, 'AtomicCmpXchgCount': 0, 'AtomicRMWCount': 0,
    'TruncCount': 1, 'ZExtCount': 5, 'SExtCount': 0, 'FPToUICount': 0,
    'FPToSICount': 0, 'UIToFPCount': 0, 'SIToFPCount': 0,
    'FPTruncCount': 0, 'FPExtCount': 0, 'PtrToIntCount': 0,
    'IntToPtrCount': 0, 'BitCastCount': 0, 'AddrSpaceCastCount': 0,
    'CleanupPadCount': 0, 'CatchPadCount': 0, 'ICmpCount': 10,
    'FCmpCount': 0, 'PHICount': 0, 'CallCount': 13, 'SelectCount': 0,
    'UserOp1Count': 0, 'UserOp2Count': 0, 'VAArgCount': 0,
    'ExtractElementCount': 0, 'InsertElementCount': 0,
    'ShuffleVectorCount': 0, 'ExtractValueCount': 0,
    'InsertValueCount': 0, 'LandingPadCount': 0, 'FreezeCount': 0}

The InstCount observation spaces are quick to compute and
lightweight. They have similar computational complexity as Autophase.

Fixes #149.
@ChrisCummins
Copy link
Contributor Author

Target //tests/llvm/service:ObservationSpacesTest was failing with a load-time error on macOS due to missing symbol __ZN4llvm24DisableABIBreakingChecksE. I've decided to remove the test since it overlaps entirely with the tests run in //tests/llvm:observation_spaces_test.

@ChrisCummins ChrisCummins merged commit 0ea2907 into development Mar 30, 2021
@ChrisCummins ChrisCummins deleted the instvec-features branch March 30, 2021 16:47
@ChrisCummins ChrisCummins mentioned this pull request Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[llvm] Add an InstCount observation space
2 participants