Tune @expect, @likely, @unlikely and @prefetch macros.

This commit is contained in:
Dmitry Atamanov
2023-08-24 17:19:09 +05:00
committed by Christoffer Lerno
parent f5fea69ef9
commit b77f254ab1
4 changed files with 33 additions and 19 deletions

View File

@@ -193,11 +193,14 @@ macro enum_by_name($Type, String enum_name) @builtin
**/
macro bool @likely(bool #value, $probability = 1.0) @builtin
{
$if $probability == 1.0:
return $$expect(#value, true);
$else
return $$expect_with_probability(#value, true, $probability);
$endif
$switch
$case env::BUILTIN_EXPECT_IS_DISABLED:
return #value;
$case $probability == 1.0:
return $$expect(#value, true);
$default:
return $$expect_with_probability(#value, true, $probability);
$endswitch
}
/**
@@ -209,11 +212,14 @@ macro bool @likely(bool #value, $probability = 1.0) @builtin
**/
macro bool @unlikely(bool #value, $probability = 1.0) @builtin
{
$if $probability == 1.0:
return $$expect(#value, false);
$else
return $$expect_with_probability(#value, false, $probability);
$endif
$switch
$case env::BUILTIN_EXPECT_IS_DISABLED:
return #value;
$case $probability == 1.0:
return $$expect(#value, false);
$default:
return $$expect_with_probability(#value, false, $probability);
$endswitch
}
/**
@@ -223,11 +229,14 @@ macro bool @unlikely(bool #value, $probability = 1.0) @builtin
**/
macro @expect(#value, expected, $probability = 1.0) @builtin
{
$if $probability == 1.0:
return $$expect(#value, ($typeof(#value))expected);
$else
return $$expect_with_probability(#value, expected, $probability);
$endif
$switch
$case env::BUILTIN_EXPECT_IS_DISABLED:
return #value == expected;
$case $probability == 1.0:
return $$expect(#value, ($typeof(#value))expected);
$default:
return $$expect_with_probability(#value, expected, $probability);
$endswitch
}
/**
@@ -249,9 +258,11 @@ enum PrefetchLocality
* @param $locality `Locality ranging from none to extremely local`
* @param $write `Prefetch for write, otherwise prefetch for read.`
**/
macro prefetch(void* ptr, PrefetchLocality $locality = VERY_NEAR, bool $write = false) @builtin
macro @prefetch(void* ptr, PrefetchLocality $locality = VERY_NEAR, bool $write = false) @builtin
{
$$prefetch(ptr, $write ? 1 : 0, $locality.ordinal);
$if !env::BUILTIN_PREFETCH_IS_DISABLED:
$$prefetch(ptr, $write ? 1 : 0, $locality.ordinal);
$endif
}
macro swizzle(v, ...) @builtin

View File

@@ -179,3 +179,6 @@ macro bool os_is_posix()
return false;
$endswitch
}
const BUILTIN_EXPECT_IS_DISABLED = $feature(DISABLE_BUILTIN_EXPECT);
const BUILTIN_PREFETCH_IS_DISABLED = $feature(DISABLE_BUILTIN_PREFETCH);

View File

@@ -7,7 +7,7 @@ fn void main()
int a;
$$prefetch(&a, 1, 3);
$$prefetch(&a, 0, 1);
prefetch(&a);
@prefetch(&a);
}
/* #expect: test.ll

View File

@@ -57,7 +57,7 @@ int abc;
fn void test_prefetch()
{
prefetch(&abc);
@prefetch(&abc);
}
fn void test_hash()