Skip to content

Commit

Permalink
Support more AtomicRWOps
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsmeding committed Jul 15, 2024
1 parent 1fa09fc commit 3d0582a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Text/LLVM/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,10 @@ data ConvOp
| BitCast
deriving (Data, Eq, Enum, Generic, Ord, Show, Typeable)

-- | Some of the atomic RW operations are supported only on later LLVM
-- versions. Because the LLVM version is chosen dynamically, the data
-- constructors cannot be excluded statically; pretty-printing for an LLVM
-- version that does not support the operation will call 'error'.
data AtomicRWOp
= AtomicXchg
| AtomicAdd
Expand All @@ -994,6 +998,12 @@ data AtomicRWOp
| AtomicMin
| AtomicUMax
| AtomicUMin
| AtomicFAdd -- ^ LLVM >= 9
| AtomicFSub -- ^ LLVM >= 9
| AtomicFMax -- ^ LLVM >= 15
| AtomicFMin -- ^ LLVM >= 15
| AtomicUIncWrap -- ^ LLVM >= 16
| AtomicUDecWrap -- ^ LLVM >= 16
deriving (Data, Eq, Enum, Generic, Ord, Show, Typeable)

data AtomicOrdering
Expand Down
23 changes: 23 additions & 0 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ ppLLVM38 = withConfig Config { cfgVer = llvmV3_8 }
llvmVer :: (?config :: Config) => LLVMVer
llvmVer = cfgVer ?config

llvmVerToString :: LLVMVer -> String
llvmVerToString 0 = "3.5"
llvmVerToString 1 = "3.6"
llvmVerToString 2 = "3.7"
llvmVerToString 3 = "3.8"
llvmVerToString n
| n >= 4 = show n
| otherwise = error $ "Invalid LLVMVer: " ++ show n

-- | This is a helper function for when a list of parameters is gated by a
-- condition (usually the llvmVer value).
when' :: Monoid a => Bool -> a -> a
Expand Down Expand Up @@ -547,6 +556,12 @@ ppAtomicOp AtomicMax = "max"
ppAtomicOp AtomicMin = "min"
ppAtomicOp AtomicUMax = "umax"
ppAtomicOp AtomicUMin = "umin"
ppAtomicOp AtomicFAdd = onlyOnLLVM 9 "AtomicFAdd" "fadd"
ppAtomicOp AtomicFSub = onlyOnLLVM 9 "AtomicFSub" "fsub"
ppAtomicOp AtomicFMax = onlyOnLLVM 15 "AtomicFMax" "fmax"
ppAtomicOp AtomicFMin = onlyOnLLVM 15 "AtomicFMin" "fmin"
ppAtomicOp AtomicUIncWrap = onlyOnLLVM 16 "AtomicUIncWrap" "uincwrap"
ppAtomicOp AtomicUDecWrap = onlyOnLLVM 16 "AtomicUDecWrap" "udecwrap"

ppScope :: Fmt (Maybe String)
ppScope Nothing = empty
Expand Down Expand Up @@ -1353,3 +1368,11 @@ structBraces body = char '{' <+> body <+> char '}'

ppMaybe :: Fmt a -> Fmt (Maybe a)
ppMaybe = maybe empty

-- | Throw an error if the ?config version is older than the given version. The
-- String indicates the constructor that is unavailable.
onlyOnLLVM :: (?config :: Config) => LLVMVer -> String -> a -> a
onlyOnLLVM fromVer name
| llvmVer >= fromVer = id
| otherwise = error $ name ++ " is supported only on LLVM >= "
++ llvmVerToString fromVer

0 comments on commit 3d0582a

Please sign in to comment.