[Git][ghc/ghc][wip/sjakobi/elem-tests] Improve test for specialization of `elem`
Simon Jakobi pushed to branch wip/sjakobi/elem-tests at Glasgow Haskell Compiler / GHC Commits: 75f2bdd4 by Simon Jakobi at 2026-03-27T16:18:24+01:00 Improve test for specialization of `elem` Fixes #27101. - - - - - 7 changed files: - − libraries/base/tests/perf/Makefile - libraries/base/tests/perf/T17752.hs - − libraries/base/tests/perf/T17752.stdout - + libraries/base/tests/perf/T17752_O0.stderr - + libraries/base/tests/perf/T17752_O1.stderr - + libraries/base/tests/perf/T17752_O2.stderr - libraries/base/tests/perf/all.T Changes: ===================================== libraries/base/tests/perf/Makefile deleted ===================================== @@ -1,15 +0,0 @@ -# This Makefile runs the tests using GHC's testsuite framework. It -# assumes the package is part of a GHC build tree with the testsuite -# installed in ../../../testsuite. - -TOP=../../../../testsuite -include $(TOP)/mk/boilerplate.mk -include $(TOP)/mk/test.mk - - -T17752: - '$(TEST_HC)' $(TEST_HC_OPTS) -O --make T17752 -rtsopts -ddump-simpl -ddump-to-file -dsuppress-uniques -dsuppress-all - # All occurrences of elem should be optimized away. - # For strings these should result in loops after inlining foldCString. - # For lists it should result in a case expression. - echo $$(grep -A4 "elem" T17752.dump-simpl) ===================================== libraries/base/tests/perf/T17752.hs ===================================== @@ -6,7 +6,7 @@ module T17752 where -- Should compile to a pattern match if the rules fire isElemList x = x `elem` ['a','b','c'] -isNotElemList x = x `elem` ['x','y','z'] +isNotElemList x = x `notElem` ['x','y','z'] isOneOfThese x = x `elem` [1,2,3,4,5::Int] isNotOneOfThese x = x `notElem` [1,2,3,4,5::Int] ===================================== libraries/base/tests/perf/T17752.stdout deleted ===================================== @@ -1,2 +0,0 @@ -[1 of 1] Compiling T17752 ( T17752.hs, T17752.o ) - ===================================== libraries/base/tests/perf/T17752_O0.stderr ===================================== @@ -0,0 +1,47 @@ +isElemList + = \ x -> + elem + $fFoldableList + $fEqChar + x + (: (C# 'a'#) (: (C# 'b'#) (: (C# 'c'#) []))) + +isNotElemList + = \ x -> + notElem + $fFoldableList + $fEqChar + x + (: (C# 'x'#) (: (C# 'y'#) (: (C# 'z'#) []))) + +isOneOfThese + = \ x -> + elem + $fFoldableList + $fEqInt + x + (: (I# 1#) (: (I# 2#) (: (I# 3#) (: (I# 4#) (: (I# 5#) []))))) + +isNotOneOfThese + = \ x -> + notElem + $fFoldableList + $fEqInt + x + (: (I# 1#) (: (I# 2#) (: (I# 3#) (: (I# 4#) (: (I# 5#) []))))) + +isElemString + = \ x -> elem $fFoldableList $fEqChar x (unpackCString# "foo"#) + +isNotElemString + = \ x -> notElem $fFoldableList $fEqChar x (unpackCString# "bar"#) + +isElemStringUtf + = \ x -> + elem $fFoldableList $fEqChar x (unpackCStringUtf8# "fo\\195\\182"#) + +isNotElemStringUtf + = \ x -> + notElem + $fFoldableList $fEqChar x (unpackCStringUtf8# "b\\195\\164r"#) + ===================================== libraries/base/tests/perf/T17752_O1.stderr ===================================== @@ -0,0 +1,118 @@ +isElemList + = \ x -> + case x of { C# x1 -> + case x1 of { + __DEFAULT -> False; + 'a'# -> True; + 'b'# -> True; + 'c'# -> True + } + } + +isNotElemList + = \ x -> + case x of { C# x1 -> + case x1 of { + __DEFAULT -> True; + 'x'# -> False; + 'y'# -> False; + 'z'# -> False + } + } + +isOneOfThese + = \ x -> + case x of { I# x1 -> + case x1 of { + __DEFAULT -> False; + 1# -> True; + 2# -> True; + 3# -> True; + 4# -> True; + 5# -> True + } + } + +isNotOneOfThese + = \ x -> + case x of { I# x1 -> + case x1 of { + __DEFAULT -> True; + 1# -> False; + 2# -> False; + 3# -> False; + 4# -> False; + 5# -> False + } + } + +isElemString + = \ x -> + joinrec { + go addr z + = case indexCharOffAddr# addr 0# of ch { + __DEFAULT -> + case x of { C# x1 -> + case eqChar# x1 ch of { + __DEFAULT -> jump go (plusAddr# addr 1#) z; + 1# -> True + } + }; + '\NUL'# -> z + }; } in + jump go isElemString1 False + +isNotElemString + = \ x -> + joinrec { + go addr z + = case indexCharOffAddr# addr 0# of ch { + __DEFAULT -> + case x of { C# x1 -> + case eqChar# x1 ch of { + __DEFAULT -> jump go (plusAddr# addr 1#) z; + 1# -> False + } + }; + '\NUL'# -> + case z of { + False -> True; + True -> False + } + }; } in + jump go isNotElemString1 False + +isElemStringUtf + = \ x -> + unpackFoldrCStringUtf8# + isElemStringUtf1 + (\ y r -> + case x of { C# x1 -> + case y of { C# y1 -> + case eqChar# x1 y1 of { + __DEFAULT -> r; + 1# -> True + } + } + }) + False + +isNotElemStringUtf + = \ x -> + case unpackFoldrCStringUtf8# + isNotElemStringUtf1 + (\ y r -> + case x of { C# x1 -> + case y of { C# y1 -> + case eqChar# x1 y1 of { + __DEFAULT -> r; + 1# -> True + } + } + }) + False + of { + False -> True; + True -> False + } + ===================================== libraries/base/tests/perf/T17752_O2.stderr ===================================== @@ -0,0 +1,130 @@ +isElemList + = \ x -> + case x of { C# x1 -> + case x1 of { + __DEFAULT -> False; + 'a'# -> True; + 'b'# -> True; + 'c'# -> True + } + } + +isNotElemList + = \ x -> + case x of { C# x1 -> + case x1 of { + __DEFAULT -> True; + 'x'# -> False; + 'y'# -> False; + 'z'# -> False + } + } + +isOneOfThese + = \ x -> + case x of { I# x1 -> + case x1 of { + __DEFAULT -> False; + 1# -> True; + 2# -> True; + 3# -> True; + 4# -> True; + 5# -> True + } + } + +isNotOneOfThese + = \ x -> + case x of { I# x1 -> + case x1 of { + __DEFAULT -> True; + 1# -> False; + 2# -> False; + 3# -> False; + 4# -> False; + 5# -> False + } + } + +isElemString + = \ x -> + case indexCharOffAddr# isElemString1 0# of ch { + __DEFAULT -> + case x of { C# x1 -> + case eqChar# x1 ch of { + __DEFAULT -> + joinrec { + go addr z + = case indexCharOffAddr# addr 0# of ch1 { + __DEFAULT -> + case eqChar# x1 ch1 of { + __DEFAULT -> jump go (plusAddr# addr 1#) z; + 1# -> True + }; + '\NUL'# -> z + }; } in + jump go (plusAddr# isElemString1 1#) False; + 1# -> True + } + }; + '\NUL'# -> False + } + +isNotElemString + = \ x -> + case indexCharOffAddr# isNotElemString1 0# of ch { + __DEFAULT -> + case x of { C# x1 -> + case eqChar# x1 ch of { + __DEFAULT -> + joinrec { + $sgo sc + = case indexCharOffAddr# sc 0# of ch1 { + __DEFAULT -> + case eqChar# x1 ch1 of { + __DEFAULT -> jump $sgo (plusAddr# sc 1#); + 1# -> False + }; + '\NUL'# -> True + }; } in + jump $sgo (plusAddr# isNotElemString1 1#); + 1# -> False + } + }; + '\NUL'# -> True + } + +isElemStringUtf + = \ x -> + unpackFoldrCStringUtf8# + isElemStringUtf1 + (\ y r -> + case x of { C# x1 -> + case y of { C# y1 -> + case eqChar# x1 y1 of { + __DEFAULT -> r; + 1# -> True + } + } + }) + False + +isNotElemStringUtf + = \ x -> + case unpackFoldrCStringUtf8# + isNotElemStringUtf1 + (\ y r -> + case x of { C# x1 -> + case y of { C# y1 -> + case eqChar# x1 y1 of { + __DEFAULT -> r; + 1# -> True + } + } + }) + False + of { + False -> True; + True -> False + } + ===================================== libraries/base/tests/perf/all.T ===================================== @@ -5,7 +5,26 @@ setTestOpts(js_skip) # Check specialization of elem via rules #-------------------------------------- -test('T17752', [only_ways(['normal'])] , makefile_test, ['T17752']) +test('T17752_O0', + [only_ways(['normal']), extra_files(['T17752.hs'])], + multimod_compile_filter, + ['T17752', + '-O0 -ddump-simpl -dsuppress-all -dsuppress-uniques -dno-typeable-binds', + "sed -n '/^is[A-Za-z]*$/,/^$/p'"]) + +test('T17752_O1', + [only_ways(['normal']), extra_files(['T17752.hs'])], + multimod_compile_filter, + ['T17752', + '-O1 -ddump-simpl -dsuppress-all -dsuppress-uniques -dno-typeable-binds', + "sed -n '/^is[A-Za-z]*$/,/^$/p'"]) + +test('T17752_O2', + [only_ways(['normal']), extra_files(['T17752.hs'])], + multimod_compile_filter, + ['T17752', + '-O2 -ddump-simpl -dsuppress-all -dsuppress-uniques -dno-typeable-binds', + "sed -n '/^is[A-Za-z]*$/,/^$/p'"]) #-------------------------------------- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/75f2bdd415f0ec589e8ec0bdef992082... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/75f2bdd415f0ec589e8ec0bdef992082... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Simon Jakobi (@sjakobi2)