| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // MutexFuncHash.hpp -- Hash utility for mutex function identity.
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Matthew Au-Yeung
|
|---|
| 10 | // Created On : Tue Jan 28 2026
|
|---|
| 11 | //
|
|---|
| 12 |
|
|---|
| 13 | #pragma once
|
|---|
| 14 |
|
|---|
| 15 | #include <cstdint>
|
|---|
| 16 | #include <string>
|
|---|
| 17 |
|
|---|
| 18 | #include "AST/Decl.hpp"
|
|---|
| 19 | #include "AST/Expr.hpp"
|
|---|
| 20 | #include "AST/Type.hpp"
|
|---|
| 21 | #include "SymTab/Mangler.hpp"
|
|---|
| 22 |
|
|---|
| 23 | namespace Concurrency {
|
|---|
| 24 |
|
|---|
| 25 | // FNV-1a hash of a function declaration's mangled name.
|
|---|
| 26 | // Used to identify mutex functions across translation units,
|
|---|
| 27 | // since function pointers may differ for static inline functions.
|
|---|
| 28 | static inline uint64_t hashMangle( const ast::DeclWithType * decl ) {
|
|---|
| 29 | std::string name = Mangle::mangle( decl );
|
|---|
| 30 | uint64_t hash = 14695981039346656037ULL; // FNV offset basis
|
|---|
| 31 | for ( char c : name ) {
|
|---|
| 32 | hash ^= static_cast<uint64_t>( c );
|
|---|
| 33 | hash *= 1099511628211ULL; // FNV prime
|
|---|
| 34 | }
|
|---|
| 35 | return hash;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | // Create a ConstantExpr for the hash with proper ULL suffix to avoid
|
|---|
| 39 | // C compiler warnings about large unsigned constants.
|
|---|
| 40 | static inline ast::ConstantExpr * hashMangleExpr(
|
|---|
| 41 | const CodeLocation & location, const ast::DeclWithType * decl ) {
|
|---|
| 42 | uint64_t hash = hashMangle( decl );
|
|---|
| 43 | return new ast::ConstantExpr{
|
|---|
| 44 | location,
|
|---|
| 45 | new ast::BasicType{ ast::BasicKind::LongLongUnsignedInt },
|
|---|
| 46 | std::to_string( hash ) + "ull",
|
|---|
| 47 | (unsigned long long)hash };
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | } // namespace Concurrency
|
|---|